I have a login.php script which will validate the username and password entered in the android. The code is below
<?php
include('dbconnect.php');
$data=file_get_contents('php://input');
$json = json_decode($data);
$tablename = "users";
//username and password sent from android
$username=$json->{'username'};
$password=$json->{'password'};
//protecting mysql injection
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$password = md5($password);
$sql = "SELECT id FROM $tablename WHERE u_username='$username' and password='$password'";
//Querying the database
$result=mysql_query($sql);
//If found, number of rows must be 1
if((mysql_num_rows($result))==1){
//creating session
session_register("$username");
session_register("$password");
print "success";
}else{
print "Incorrect details";
}
?>
I also have an android class from which the user will enter the username and password. The code is below.
public class LoginActivity extends Activity {
public static final String loginURI="http://.../login.php";
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
buttonSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String userID = "";
userID=login(editTextUsername.getText().toString(), editTextPassword.getText().toString());
if (editTextPassword.getText().toString() != null & editTextUsername.getText().toString() != null){
//Used to move to the Cases Activity
Intent casesActivity = new Intent(getApplicationContext(), CasesActivity.class);
startActivity(casesActivity);
casesActivity.putExtra("username", userID);
}
else{
//Display Toaster for error
Toast.makeText(getApplicationContext(),"this is an error message", Toast.LENGTH_LONG).show();
}
}
});
private String login(String username, String password){
JSONObject jsonObject = new JSONObject();
String success = "";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(loginURI);
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,10000);
HttpConnectionParams.setSoTimeout(httpParams,10000);
try {
jsonObject.put("username", username);
Log.i("username", jsonObject.toString());
jsonObject.put("password", password);
Log.i("password", jsonObject.toString());
StringEntity stringEntity = new StringEntity(jsonObject.toString());
stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(stringEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
success = EntityUtils.toString(httpResponse.getEntity());
Log.i("success", success);
}
}catch (IOException e){
Log.e("Login_Issue", e.toString());
}catch (JSONException e) {
e.printStackTrace();
}
return success;
}
}
I get the following error: ERROR/AndroidRuntime(29611): FATAL EXCEPTION: main android.os.NetworkOnMainThreadException.
I understand that I need another thread and I was thinking of using AsyncTask, but I do not know where to put it in this class.
Could you also give me some advice in using JSON for sending and receiving data from android.
Thank you for your help,
you can change your code using AsyncTask by calling login method inside
doInBackgroundand start next Activity ononPostExecutewhen login successful as :and start LoginOperation AsyncTask on button click as: