2 nik666 nik666 于 2016.03.08 09:46 提问

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

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

3个回答

nik666
nik666   2016.03.08 09: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();
    }

}

nik666
nik666 这是我的代码 提问时忘了贴上去
接近 2 年之前 回复
dongfeng9ge
dongfeng9ge   Ds   Rxr 2016.03.08 10:50

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

v331687828
v331687828   2016.03.08 11:19

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

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!