JSONException:值&LT; java.lang.String类型的BR不能转换为JSONArray [英] JSONException: Value <br of type java.lang.String cannot be converted to JSONArray
问题描述
我知道,这种问题有一些解决方案,并申请了这个问题的一些解决方案,但我解决不了,我很困惑。请帮帮我。这里是code:
保护字符串doInBackground(布尔... PARAMS){ 字符串结果= NULL; StringBuilder的SB =新的StringBuilder();
尝试{ // HTTP POST
HttpClient的HttpClient的=新DefaultHttpClient(); HTTPGET httppost =新HTTPGET(
http://192.168.2.245/getProducts.php?login=1&user_name=UserName&password=Password); HTT presponse响应= httpclient.execute(httppost);
如果(response.getStatusLine()的getStatus code()!= 200){
Log.d(MyApp的,服务器遇到错误);
} 读者的BufferedReader =新的BufferedReader(新的InputStreamReader(
。response.getEntity()的getContent(),UTF-8),8); //旧字符集ISO-8859-1 SB =新的StringBuilder(); sb.append(reader.readLine()+\\ n); 串线= NULL; 而((行= reader.readLine())!= NULL){ sb.append(行+\\ n);
}
结果= sb.toString(); Log.d(测试,结果); }
赶上(例外五){ Log.e(log_tag,错误转换结果+ e.toString()); } 返回结果;
}
PHP code:
$登录= $ _ GET [登录];
$ USER_NAME = $ _ GET [USER_NAME];
$密码= $ _ GET [密码];
$输出=阵列();
如果($登录){
$ SQL =的mysql_query(SELECT USER_ID用户的其中用户名='$ USER_NAME。'和user_pass =$的密码。');而($行= mysql_fetch_array($ SQL)){
$ USER_ID = $行[user_ID的];
}$ SQL =的mysql_query(SELECT姓名,DEVICE_ID,纬度,经度从设备中的WHERE user_ID的='LIMIT 100$ user_ID的。');而($行= mysql_fetch_assoc($ sql中)){
$输出[] = $行; }
}打印(json_en code($输出));
mysql_close();
在logcat的:org.json.JSONException:值
我应该怎么办? PHP的code网页上运行,没有任何错误,但为什么得到这部分的错误?
在logcat中也有:
通知(!):未定义的变量:USER_ID在C:\\ WAMP \\ WWW \\ getProducts.php在线 27
logcat的!
&LT; BR /&GT;&LT;字体大小='1'&GT;&LT;表类='Xdebug的错误XE-通知书DIR ='升'的边界='1 CELLSPACING ='0'的cellpadding =1&GT;&LT; TR&GT;百分位ALIGN =左的bgcolor =#f57900合并单元格=5&GT;&LT;跨度风格=背景颜色:#CC0000 ;颜色:#fce94f;字体大小:X-大;'&GT;&LT; / SPAN&GT;(!)注意:未定义的变量:USER_ID在C:\\ WAMP \\ WWW \\ getProducts.php上线474; I&GT; 27&LT; I&GT /;&LT; /第i&LT; / TR&GT;&LT; TR&GT;百分位ALIGN =左 BGCOLOR ='#e9b96e合并单元格=5&GT;调用堆栈和LT; /第i&LT; / TR&GT; ...这种code的出现在logcat的话org.json.JSONException:值&LT; BR型java.lang.String中不能在org.json.JSON.typeMismatch转换为JSONArray(JSON.java:107)
您不会返回一个有效的JSON,看来你是返回一个HTML片段&LT; BR&GT;
看看你的网站的你回归源头。
我应该怎么办? PHP的code网页上运行,没有任何错误,但为什么得到这部分的错误?在logcat中也有:(!)注意:未定义的变量:USER_ID在C:\\ WAMP \\ WWW \\ getProducts.php第27行
块引用>所以
USER_ID
没有定义!只要看看你的GET / POST参数或告诉我们您的相关PHP code。请使用
回声json_en code($输出);
不是打印
和你在做什么如果从第一个查询不可能返回的行。
试试这个。$ SQL =的mysql_query(SELECT USER_ID用户的其中用户名='$ USER_NAME。'和user_pass =$的密码。');
如果(mysql_num_rows($ SQL)== 0){
回声USERID无法找到;
}
而($行= mysql_fetch_array($ SQL)){
回声USERID找到$行[user_ID的]。
$ USER_ID = $行[user_ID的];
}似乎
USER_NAME
找不到。所以,只是尝试一段时间后你的呼应USER_ID调试它。如果你没有得到的用户名FOUND回声user_name和密码不存在。编辑:正如你说logact,有没有为进入你逝去的,以使$ USER_ID将永远无法填补
的用户名。
<$c$c>http://192.168.2.245/getProducts.php?login=1&user_name=UserName&password=Password$c$c>
块引用>您逝去的USER_NAME =用户名和密码=密码。这是否真的进入数据库中的存在吗?
也有更好的表现来看看MySQL的LETF JOIN和MySQL的子查询。
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:
$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
What should i do? The Php code runs on web page without any errors but why got an error in this part? In logcat also has: ( ! ) Notice: Undefined variable: user_id in C:\wamp\www\getProducts.php on line 27
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.What should i do? The Php code runs on web page without any errors but why got an error in this part? In logcat also has: ( ! ) Notice: Undefined variable: user_id in C:\wamp\www\getProducts.php on line 27
So the
user_id
is not defined! Just look at your GET/POST Parameters or show us your relevant php code.Please use
echo json_encode($output);
notAnd 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. So just debug it there by trying to echo your user_id after the while. If you do not get the UserID FOUND echo the user_name and password do not exist.Edit: As your logact says, there is no Entry for the username you are passing so the $user_id will never be filled.
http://192.168.2.245/getProducts.php?login=1&user_name=UserName&password=Password
You are passing user_name = UserName and password = Password. Does this entry really exist in your Database?
Also for better performance have a look at MySQL LETF JOIN and MySQL subqueries.
这篇关于JSONException:值&LT; java.lang.String类型的BR不能转换为JSONArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!