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.

    评论

报告相同问题?

悬赏问题

  • ¥20 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏