[英]JSONException: Value <br of type java.lang.String cannot be converted to JSONArray
I know that this kind of problem has some solutions and applied some solutions for this problem but I can't solve and i'm confused. 我知道这种问题有一些解决方案,并且对此问题应用了一些解决方案,但我无法解决,我很困惑。 Please help me.
请帮我。 Here is code:
这是代码:
protected String doInBackground(Boolean... params) {
String result = null;
StringBuilder sb = new StringBuilder();
try {
// http post
HttpClient httpclient = new DefaultHttpClient();
HttpGet httppost = new HttpGet(
"http://192.168.2.245/getProducts.php?login=1&user_name=UserName&password=Password");
HttpResponse response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() != 200) {
Log.d("MyApp", "Server encountered an error");
}
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent(), "utf-8"), 8); //old charset iso-8859-1
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
Log.d("test", result);
}
catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
return result;
}
PHP CODE: PHP代码:
$login=$_GET["login"];
$user_name=$_GET["user_name"];
$password=$_GET["password"];
$output=array();
if ($login) {
$sql=mysql_query("SELECT user_id FROM users WHERE user_name='".$user_name."' AND user_pass='".$password."' ");
while($row=mysql_fetch_array($sql)) {
$user_id=$row["user_id"];
}
$sql=mysql_query("SELECT name,device_id,lat,lon FROM devices WHERE user_id='".$user_id."' LIMIT 100");
while($row=mysql_fetch_assoc($sql)) {
$output[]=$row; }
}
print(json_encode($output));
mysql_close();
The logcat:org.json.JSONException: Value logcat:org.json.JSONException:值
What should i do? 我该怎么办? The Php code runs on web page without any errors but why got an error in this part?
Php代码在网页上运行时没有任何错误,但是为什么这部分出错了? In logcat also has: ( ! ) Notice: Undefined variable: user_id in C:\\wamp\\www\\getProducts.php on line 27
在logcat中还具有:(!)注意:未定义的变量:C:\\ wamp \\ www \\ getProducts.php在第27行的user_id
Logcat! logcat的!
<br /><font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: user_id in C:\wamp\www\getProducts.php on line <i>27</i></th></tr><tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>... this kind of code appears in logcat then org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONArray at org.json.JSON.typeMismatch(JSON.java:107)
You do not return a valid JSON, seems that you are returning a HTML snippet with <br>
have a look at your returning source of your website. 您没有返回有效的JSON,似乎您正在返回带有
<br>
的HTML代码段,请查看您网站的返回源。
What should i do?
我该怎么办? The Php code runs on web page without any errors but why got an error in this part?
Php代码在网页上运行时没有任何错误,但是为什么这部分出错了? In logcat also has: ( ! ) Notice: Undefined variable: user_id in C:\\wamp\\www\\getProducts.php on line 27
在logcat中还具有:(!)注意:未定义的变量:C:\\ wamp \\ www \\ getProducts.php在第27行的user_id
So the user_id
is not defined! 因此未定义
user_id
! Just look at your GET/POST Parameters or show us your relevant php code. 只需查看您的GET / POST参数或向我们显示您相关的php代码即可。
Please use echo json_encode($output);
请使用
echo json_encode($output);
not print
. 不
print
。
And what are you doing if there is no returning row from your first Query. 如果您的第一个查询没有返回行,您将如何处理。 Try this.
尝试这个。
$sql=mysql_query("SELECT user_id FROM users WHERE user_name='".$user_name."' AND user_pass='".$password."' ");
if(mysql_num_rows($sql) == 0){
echo "USERID CANNOT BE FOUND";
}
while($row=mysql_fetch_array($sql)) {
echo "USERID FOUND" .$row["user_id"] ;
$user_id=$row["user_id"];
}
It seems that user_name
could not be found. 似乎找不到
user_name
。 So just debug it there by trying to echo your user_id after the while. 因此,只需在一段时间后尝试回显您的user_id即可对其进行调试。 If you do not get the UserID FOUND echo the user_name and password do not exist.
如果没有得到USERID FOUND回显,则user_name和密码不存在。
Edit: As your logact says, there is no Entry for the username you are passing so the $user_id will never be filled. 编辑:正如您的logact所说,您传递的用户名没有条目,因此$ user_id将永远不会被填充。
http://192.168.2.245/getProducts.php?login=1&user_name=UserName&password=Password
You are passing user_name = UserName and password = Password. 您正在传递user_name = UserName和password = Password。 Does this entry really exist in your Database?
该条目确实存在于您的数据库中吗?
Also for better performance have a look at MySQL LETF JOIN and MySQL subqueries. 为了获得更好的性能,请查看MySQL LETF JOIN和MySQL子查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.