nik666 2016-02-28 15:19 采纳率: 33.3%
浏览 2122
已采纳

android app通过php API连接数据库实现注册问题

<?php
$conn = mysql_connect("localhost", "root", "root");
?>

<?php
require 'conn.php';

if($_POST['username']!=""&&$_POST['password']!="")
{
$password=md5($_POST['password']);
$sql="select * from 'user' where username='$_POST[username]'";
$query=mysqli_query($conn,$sql);
$row=mysqli_num_row($query);
if($row==0){
$sql2="insert into 'user' (username,password) values('$_POST[username]','$password')";
$query=mysqli_query($conn,$sql2);
$result=array("status"=>"success");
echo json_encode($result);
}
else{
$result=array("status"=>"exists");
echo json_encode($result);
}
}

?>

public class register extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    CustomTitle ct= new CustomTitle();
    ct.getTitleBar(this, "ע��",R.drawable.oval,R.drawable.oval,"����","ȡ��");
    setContentView(R.layout.signup);

    Button button1=(Button)findViewById(R.id.button1);
    final EditText editText1=(EditText)findViewById(R.id.editText1);
    final EditText editText2=(EditText)findViewById(R.id.editText2);

    button1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String username=editText1.getText().toString();
            String password=editText2.getText().toString();

            if(username.equals("")||password.equals("")){
                 Toast.makeText(register.this, "用户名或密码不能为空", Toast.LENGTH_LONG);
            }
            else{
                AsyncHttpClient client=new AsyncHttpClient();
                RequestParams params=new RequestParams();
                params.add("username",username);
                params.add("password",password);
                client.post("http://192.168.1.203/NewsDemo/register.php",params,new AsyncHttpResponseHandler() {

                    @Override
                    public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
                        // TODO Auto-generated method stub
                        String response=new String();
                        Log.e("debug",response);
                        JSONObject object=null;
                        try {
                            object=new JSONObject(response);
                            String status=object.getString("status");
                            if(status.equals("exists")){
                                Toast.makeText(register.this, "用户名已存在,请更换", Toast.LENGTH_LONG);
                                }else if(status.equals("error")){
                                    Toast.makeText(register.this, "出现错误请稍后重试", Toast.LENGTH_LONG);
                                }
                                else{

                                    App.isLogin=true;
                                    Intent intent=new Intent(register.this,MainActivity.class);
                                    startActivity(intent);
                                    register.this.finish();
                                }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
                        // TODO Auto-generated method stub
                        Toast.makeText(register.this, "网络错误", Toast.LENGTH_LONG);
                    }
                });

            }
        }
    });

}

}

请问这段代码有什么问题吗 我按注册键也就是button1 没有反应 数据库log有收到信息 没有反馈
92.168.1.203 - - [28/Feb/2016:22:47:44 +0800] "POST /NewsDemo/register.php HTTP/1.1" 200 297 "-" "-"
192.168.1.203 - - [28/Feb/2016:22:47:45 +0800] "POST /NewsDemo/register.php HTTP/1.1" 200 297 "-" "-"
192.168.1.203 - - [28/Feb/2016:22:47:45 +0800] "POST /NewsDemo/register.php HTTP/1.1" 200 297 "-" "-"
192.168.1.203 - - [28/Feb/2016:22:47:47 +0800] "POST /NewsDemo/register.php HTTP/1.1" 200 297 "-" "-"
192.168.1.203 - - [28/Feb/2016:22:47:47 +0800] "POST /NewsDemo/register.php HTTP/1.1" 200 297 "-" "-"
192.168.1.203 - - [28/Feb/2016:22:49:31 +0800] "POST /NewsDemo/register.php HTTP/1.1" 200 297 "-" "-"
192.168.1.203 - - [28/Feb/2016:22:49:32 +0800] "POST /NewsDemo/register.php HTTP/1.1" 200 297 "-" "-"
192.168.1.203 - - [28/Feb/2016:22:49:34 +0800] "POST /NewsDemo/register.php HTTP/1.1" 200 297 "-" "-"

  • 写回答

1条回答 默认 最新

  • u52983610 2016-02-29 03:05
    关注

    String response=new String();

    这里response是个空的String?应该取arg2的值吧

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何构建全国统一的物流管理平台?
  • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
  • ¥20 和学习数据的传参方式,选择正确的传参方式有关
  • ¥15 这是网络安全里面的poem code
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)
  • ¥50 python写segy数据出错
  • ¥20 关于线性结构的问题:希望能从头到尾完整地帮我改一下,困扰我很久了
  • ¥30 3D多模态医疗数据集-视觉问答
  • ¥20 设计一个二极管稳压值检测电路