这是AsyncTask中的登录代码,即使用户存在,也会在returnUser上为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);
?>
答案 0 :(得分:0)
对不起!我很抱歉。问题很简单,即使它让我疯了一个星期!查看获取文件&#34; Fetch_User_Data.php&#34;然后查看字符串,我称之为&#34; FetchUserData.php&#34;而且你会完成......