nik666
nik666
采纳率33.3%
2016-03-08 01:46 阅读 1.5k

关于android客户端和mysql通过php完成注册功能的问题

自己正在自学中 php还不太会 求一个php的程序可以接收我下面代码的json 并且验证username是否存在 不存在则可以插入username和password~也顺便帮我看看代码哪里有什么问题之类的谢谢了!!!!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • nik666 nik666 2016-03-08 01:47

    public class Register_ extends Activity{
    private EditText register_username;
    private EditText register_passwd;
    private EditText register_repasswd;
    private Button register_submit;
    public static final int SHOW_RESPONSE=0;

       private class MessageHandler extends Handler
        {
            //持有activity的弱引用 避免内存泄漏
            private final WeakReference<Register_> weakReference;
            public MessageHandler(Register_ activity)
            {
                this.weakReference = new WeakReference<Register_>(activity);
            }
            public void handleMessage(Message msg)
            {
                Register_ activity = weakReference.get();
                //如果activity已经在处于被销毁状态 或者已经被销毁了 不再处理消息
                if (activity == null || activity.isFinishing())
                {
                    return;
                }
                switch (msg.what)
                {
                    case SHOW_RESPONSE:
                        String response = (String)msg.obj;
                        try
                        {
                            JSONObject jsonObject = new JSONObject(response);
                            Boolean error = jsonObject.getBoolean("error");
                            String message = jsonObject.getString("msg");
                            String result = jsonObject.getString("result");
                            JsonBack jsonBack = new JsonBack(error,message,result);
                            Log.d("Register_",jsonBack.toString());
                            if(jsonBack.isError())
                            {
                                Toast.makeText(Register_.this, jsonBack.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                            if(!jsonBack.isError())
                            {
                                Toast.makeText(Register_.this, "注册成功", Toast.LENGTH_SHORT).show();
                                //onPause();
                                Intent intent = new Intent(Register_.this, Login.class);
                                startActivity(intent);
                                finish();
                            }
                        }catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                }
            }
        }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.signup);
         register_username=(EditText)findViewById(R.id.editText1);
         register_passwd=(EditText)findViewById(R.id.editText2);
         register_repasswd=(EditText)findViewById(R.id.editText3);
         register_submit=(Button)findViewById(R.id.button1);
         register_submit.setOnClickListener(new View.OnClickListener() {
    
            @Override
             public void onClick(View v)
            {
                if (v.getId() == R.id.button1)
                {
                    String username = register_username.getText().toString();
                    String password = register_passwd.getText().toString();
                    String repassword = register_repasswd.getText().toString();
                    if(password.equals(repassword))
                    {
                        //使用POST方法向服务器发送数据
                        sendRequstWithHttpClient(username, password);
                    }
                    else {
    
                        Toast.makeText(Register_.this, "两次输入的密码不一样", Toast.LENGTH_SHORT).show();
                    }
                }
            }
    
    
        });    
     }
     private Handler messageHandler=new MessageHandler(this);
    
    private void sendRequstWithHttpClient(final String username,
            final String password) {
         new Thread(new Runnable() {
    
             @Override
                public void run()
                {
                    try
                    {
                        HttpClient httpClient = new DefaultHttpClient();
                        String url = "192.168.1.203/EnjoyDemo/register";
                        //第二步:生成使用POST方法的请求对象
                        HttpPost httpPost = new HttpPost(url);
                        //NameValuePair对象代表了一个需要发往服务器的键值对
                        NameValuePair pair1 = new BasicNameValuePair("username", username);
                        NameValuePair pair2 = new BasicNameValuePair("password", password);
                        //将准备好的键值对对象放置在一个List当中
                        ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
                        pairs.add(pair1);
                        pairs.add(pair2);
                        try
                        {
                            //创建代表请求体的对象(注意,是请求体)
                            HttpEntity requestEntity = new UrlEncodedFormEntity(pairs, "utf-8");
                            //将请求体放置在请求对象当中
                            httpPost.setEntity(requestEntity);
                            //执行请求对象
                            try
                            {
                                //第三步:执行请求对象,获取服务器发还的相应对象
                                HttpResponse response = httpClient.execute(httpPost);
                                //第四步:检查相应的状态是否正常:检查状态码的值是200表示正常
                                if (response.getStatusLine().getStatusCode() == 200)
                                {
                                    //第五步:从相应对象当中取出数据,放到entity当中
                                    HttpEntity entity = response.getEntity();
                                    BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
                                    String result = reader.readLine();
                                    Message message = new Message();
                                    message.what = SHOW_RESPONSE;
                                    message.obj = result;
                                    messageHandler.sendMessage(message);
                                    //Log.d("HTTP", "post:" + result);
                                }
                            } catch (Exception e)
                            {
                                e.printStackTrace();
                            }
                        } catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                    }catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    

    }

    点赞 评论 复制链接分享
  • dongfeng9ge 东风玖哥 2016-03-08 02:50

    你要找的是不是《Android和PHP最佳实践》?

    点赞 评论 复制链接分享
  • v331687828 xxxxxxxxxxxczxc 2016-03-08 03:19

    找个网络框架用用吧,你这效率太低,PHP一样是抛接口给你,username什么的为空需要客户端也就是android来判断,然后提交,回调接受服务器返回的json。

    点赞 评论 复制链接分享

相关推荐