douri4459 2015-07-22 21:31
浏览 72

将数据发送到mysql

I want to build register app with mysql … send data from android app to php file then to mysql The problem is the android app doesnt sent the data to php!! The php file is correct but the wrong in the android code……. Where is the wrong.... This is my code

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条回答 默认 最新

  • duanjia3187 2015-07-22 22:00
    关注

    These steps might help you solve your problem:

    1- Your Android device and your pc that you are trying to connect should be on the same network.

    2- Make sure you include <uses-permission android:name="android.permission.INTERNET"/> in the manifest file.

    3- It is better to use AsyncTask when doing some task that might take longer processing time like connecting to a database.

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序