public class fetchUserDataAsyncTask extends AsyncTask{
User user;
GetUserCallback userCallBack;
public fetchUserDataAsyncTask(User user, GetUserCallback userCallBack) {
this.user = user;
this.userCallBack = userCallBack;
}
@Override
protected User doInBackground(Void... params) {
ArrayListdataToSend = new ArrayList<>();
dataToSend. add(new BasicNameValuePair("username", user.username));
dataToSend.add(new BasicNameValuePair("password", user.password));
HttpParams httpRequestParams = new BasicHttpParams( );
HttpConnectionParams.setConnectionTimeout(httpRequestParams,
CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams,
CONNECTION_TIMEOUT);
Http Client client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADDRESS
+ "FetchUserData.php");
User returnedUser = null;
< br /> try {
post.setEntity(new UrlEncodedFormEntity(dataToSend));
HttpResponse httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity() ;
String result = EntityUtils.toString(entity);
JSONObject jObject = new JSONObject(result);
if (jObject.length() != 0){
Log.v("happened", "2");
String name = jObject.getString("name");
int age = jObject.getInt("age");
returnedUser = new User(name, age, user.username, user.password);
}
} catch (Exception e) {
e.printStackTrace();
}
return returnedUser;
}
@Override
protected void onPostExecute(User returnedUser) {
super.onPostExecute(returnedUser);
progressDialog. dismiss();
userCallBack.done(returnedUser);
}
}
Php gets the file Fetch_User_Data.php
< pre>{
echo “Failed to connect to MySQL: “, mysqli_connect_error();
}
if (isset($_POST[“password”])) { $password = $_POST
[“password”];}else $password =’bimbomix’;
if (isset($_POST[“username”])) {$username = $ _POST
[“username”];}else $username =’bimbomix’;
$statement = mysqli_prepare($con, “SELECT * FROM user WHERE
username =? AND password = ?”);
mysqli_stmt_bind_param($statement, “ss”, $username,
$password);
mysqli_stmt_execute($st atement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $user_id, $name, $age,
$username, $password);
$user = array();
while(mysqli_stmt_fetch($statement)){
$user[‘name’] = $name;
$user[ ‘age’] = $age;
$user[‘username’] = $username;
$user[‘password’]= $password;
}
echo json_encode($user);
mysqli_close($con);
?>
Hard coded test
User returnedUser=null;
try {
post.setEntity(new UrlEncodedFormEntity(dataToSend));
HttpResponse httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
//until here hardcoding fills correctly returnedUser
JSONObject jObject = new JSONObject(result);
returnedUser = new User("hello", 42, user.username, user.password);
// this is hardcoded string that let returnedUser ==null
// so perhaps problem is in jObject
if (jObject.length () != 0){
Log.v("happened", "2");
String name = jObject.getString("name");
int age = jObject.getInt ("age");
// returnedUser = new User(name, age, user.username, user.password);
}
} catch (Exception e) {
e.printStackTrace();
}
return returnedUser;
}
Follow my course
public class fetchUserDataAsyncTask extends AsyncTask{
User user;
GetUserCallback userCallBack;
public fetchUserDataAsyncTask(User user, GetUserCallback userCallBack) {
this.user = user;
this.userCallBack = userCallBack;
}
@Override
protected User doInBackground(Void ... params) {
ArrayListdataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("username", user.username));
dataToSend. add(new BasicNameValuePair("password", user.password));
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams,
CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams,
CONNECTION_TIMEOUT);
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADDRESS
+ "FetchUserData.php");
User returnedUser = null;
try {
post.setEntity(new UrlEncodedFormEntity(dataToSend));
HttpResponse httpResponse = client.execute(post);
< br /> HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
JSONObject jObject = new JSONObject(result);
if (jObject .length() != 0){
Log.v("happened", "2");
String name = jObject.getString("name");
int age = jObject .getInt("age");
returnedUser = new User(name, age, user.username, user.password);
}
} catch (Exception e) {
e.printStackTrace();
}
return returnedUser;
}
@Override
protected void onPostExecute(User returnedUser) {< br /> super.onPostExecute(returnedUser);
progressDialog.dismiss();
userCallBack.done(returnedUser);
}
}
Get Php File Fetch_User_Data.php
$con=mysqli_connect("localhost","root","","loginregister");
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: ", mysqli_connect_error();
}
if (isset($_POST["password"])) {$password = $_POST
["password"];}else $password ='bimbomix';
if ( isset($_POST["username"])) {$username = $_POST
["username"];}else $username ='bimbomix';
$statement = mysqli_prepare($con, "SELECT * FROM user WHERE
username =? AND password = ?");
mysqli_stmt_bind_param($statement, "ss" , $username,
$password);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $user_id, $name, $age,
$username, $password);
$user = array();
while(mysqli_stmt_fetch($statement)){
$user ['name'] = $name;
$user['age'] = $age;
$user['username'] = $username;
$user['password'] = $password;
}
echo json_encode($user);
mysqli_close($con);
?>
Hard coded test
User returnedUser=null;
try {
post.setEntity(new UrlEncodedFormEntity(dataToSend));
HttpResponse httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
//until here hardcoding fills correctly returnedUser
JSONObject jObject = new JSONObject(result) ;
returnedUser = new User("hello", 42, user.username, user.password);
// this is hardcoded string that let returnedUser ==null
// so perhaps problem is in jObject
if (jObject.length() != 0){
Log.v("happened", "2");
String name = jObject.getString ("name");
int age = jObject.getInt("age");
// returnedUser = new User(name, age, user.username, user.password);
}
} catch (Exception e) {
e.printStackTrace();
}
return returnedUser;
}
I’m sorry. The problem is simple, even if it drives me crazy for a week! Check the fetch file “Fetch_User_Data.php”, then check the string, I will call it “FetchUserData.php”, you will be done…