【问题标题】:Xampp android data fetching not workingXampp android数据获取不起作用
【发布时间】:2015-12-07 08:06:53
【问题描述】:

这是 AsyncTask 中的登录代码,即使用户存在,返回的用户也会返回 null

 @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");

        String result=null;
        User returnedUser=null;
        try {

            post.setEntity(new UrlEncodedFormEntity(dataToSend));

            HttpResponse httpResponse = client.execute(post);

            HttpEntity entity = httpResponse.getEntity();

           result = EntityUtils.toString(entity);

//returnedUser = new User(result, 42, user.username, user.password);


        } catch (Exception e) {
            e.printStackTrace();
        }



            try {
                JSONObject jObject = new JSONObject(result);
                String name = jObject.getString("name");
                int age = jObject.getInt("age");
returnedUser = new User(name, age, user.username, user.password);

            } catch (JSONException e) {
                e.printStackTrace();
            }


        return returnedUser;
    }

如果我对返回用户进行硬编码以查看结果内容,它会返回以下 html 代码。这是什么意思?

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> 
<head> 
<title>Object not found!</title> 
<link rev="made" href="mailto:postmaster@localhost" /> 
<style type="text/css"><!-- 
body { color: #000000; background-color: #FFFFFF; } 
a:link { color: #0000CC; } 
p, address {margin-left: 3em;} 
span {font-size: smaller;} 
/*]]>*/--></style> 
</head> 

<body> 
<h1>Object not found!</h1> 
<p> 


The requested URL was not found on this server. 



If you entered the URL manually please check your 
spelling and try again. 



</p> 
<p> 
If you think this is a server error, please contact 
the <a href="mailto:postmaster@localhost">webmaster</a>. 

</p> 

<h2>Error 404</h2> 
<address> 
<a href="/">192.168.1.134</a><br /> 
<span>Apache/2.4.9 (Win32) OpenSSL/1.0.1g PHP/5.5.11</span> 
</address> 
</body> 
</html>

我附上 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);

?>

【问题讨论】:

    标签: php android mysqli android-asynctask xampp


    【解决方案1】:

    对不起!我很抱歉。问题很简单,即使它让我发疯了两个星期!查看获取文件“Fetch_User_Data.php”,然后查看我称之为“FetchUserData.php”的字符串,您将完成...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-12
      • 2013-10-31
      • 2021-03-15
      • 1970-01-01
      • 2018-04-07
      • 2014-09-25
      • 1970-01-01
      相关资源
      最近更新 更多