Следует за моим занятием
public class fetchUserDataAsyncTask extends AsyncTask<Void, Void, User> {
User user;
GetUserCallback userCallBack;
public fetchUserDataAsyncTask(User user, GetUserCallback userCallBack) {
this.user = user;
this.userCallBack = userCallBack;
}
@Override
protected User doInBackground(Void... params) {
ArrayList<NameValuePair> dataToSend = 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);
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 Fetch_User_Data.php
<?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);
?>
Тест жесткого кодирования
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;
}
Я так виноват. Вопрос был простой, даже если он сошел с ума на пару недель! Посмотрите на файл извлечения «Fetch_User_Data.php», а затем посмотрите на строку, где я называю его «FetchUserData.php», и все будет готово …
Других решений пока нет …