将数据发送到mysql

时间:2015-07-22 21:31:30

标签: php android mysql

我想用mysql构建注册应用程序 ...将数据从Android应用程序发送到php文件,然后发送到mysql 问题是Android应用程序没有将数据发送到PHP! php文件是正确的,但在android代码中错了....... 哪里错了...... 这是我的代码

public class Login extends Activity {
EditText et, pass;
TextView text1;
String send_num;
HttpPost httppost;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    et = (EditText) findViewById(R.id.user);
    pass = (EditText) findViewById(R.id.pass);
    text1 = (TextView) findViewById(R.id.text1);
}

public void onClick(View v) {
    dialog = ProgressDialog.show(Login.this, "",
            "Waiting ...", true);
    new Thread(new Runnable() {
        public void run() {
            login();  
        }
    }).start();
}

void login() {
    try {

        httpclient = new DefaultHttpClient();
        httppost = new HttpPost("http://192.168.1.10/sync.php"); 

        nameValuePairs = new ArrayList<>(2);            nameValuePairs.add(new BasicNameValuePair("username", et.getText().toString().trim()));
        nameValuePairs.add(new BasicNameValuePair("password", pass.getText().toString().trim()));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        response = httpclient.execute(httppost);

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        final String response = httpclient.execute(httppost, responseHandler);
        runOnUiThread(new Runnable() {
            public void run() {
                text1.setText(response); 
                dialog.dismiss();
            }
        });

        if (response.equalsIgnoreCase("User Found")) {           runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(Login.this, "login suc", Toast.LENGTH_SHORT).show();
                }
            });            Intent i = new Intent(Login.this, Activity_Main.class);
            send_num = et.getText().toString();
            i.putExtra("text", send_num);
            startActivity(i);
        } else {
        }

    } catch (Exception e) {
    }
}
}

1 个答案:

答案 0 :(得分:0)

这些步骤可能会帮助您解决问题:

1-您尝试连接的Android设备和PC应位于同一网络上。

2-确保在清单文件中包含<uses-permission android:name="android.permission.INTERNET"/>

3-在执行某些可能需要更长处理时间的任务(如连接到数据库)时,最好使用AsyncTask。