[英]getting error :Error parsing data org.json.JSONException: Value of type org.json.JSONArray cannot be converted to JSONObject
Stuck and need help.卡住了,需要帮助。 JSON parser cannot convert data so I can use the username and password to login.
JSON 解析器无法转换数据,所以我可以使用用户名和密码登录。
jsonParser = new JSONParser();
public JSONObject loginUser(String username, String password) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", login_tag));
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
return json;
}
PHP part of the code: PHP部分代码:
while($r = mysql_fetch_assoc($result)) {
$json[] = $r;
}
print json_encode($json);
With the comments of the @njzk2 the solution should be to change your PHP coding:根据@njzk2 的评论,解决方案应该是更改您的 PHP 编码:
But then you just get the first:但是你只会得到第一个:
while($r = mysql_fetch_assoc($result)) {
$json = $r;
break;
}
print json_encode($json);
Or change your Java Coding to a JsonArray或者将您的 Java 编码更改为 JsonArray
jsonParser = new JSONParser();
public JSONArray loginUser(String username, String password) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", login_tag));
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
// change the following line, to the correct method. Which parser do you use?
JSONArray json = jsonParser.getJSONArrayFromUrl(loginURL, params);
return json;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.