Android登录与json对象空在抓取即使php文件执行浏览器Chrome是ok的


Android login with json object empty in fetching even if php file execution in browser Chrome is ok

跟随我的抓取类

 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你就完成了。