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) {
}
}
}
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.