我想将json
字符串转换为JSONArray,但它会抛出jsonexception
这很奇怪,因为它给我的错误似乎完全是错误的调试。
这是代码的重要部分:
...
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
// convert response to string
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "utf-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
String result = sb.toString();
result.trim(); //added by recommendation in comments
// parse json data
try {
JSONArray jArray = new JSONArray(result); **//this line jumps to the exception**
//Also tried
//JSONTokener tokener = new JSONTokener(result);
//JSONArray jArray = new JSONArray(tokener); //jumps to the exception too
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
TextView info = (TextView)findViewById(R.id.info);
String sInfo = "id: "
+ json_data.getInt("ID")
+ ", descripció: "
+ json_data.getString("DESC")
+ ", nick: "
+ json_data.getString("NICK")
+ ", data de naixement: "
+ json_data.getString("DATA_NEIX");
info.setText(sInfo);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data "
+ e.toString());
}
这是调试时得到的异常的详细信息:
A JSONArray text must start with '[' at character 1 of [{"ID":"1","NICK":"Jauuu","DESC":"Estic de proves amb php i mysql","FOTO":null,"DATA_NEIX":"1980-04-22","IDIOMA":null,"PAIS":"Espanya","GENERE":"H","ORIENTACIO":"heterosexu","ID_GRUP":null,"ESTAT":null,"ID_AMICS":null}]
正如你所看到的那样,角色1确实存在'['
。我确信这会按位置观察弦乐的位置。所以我无法确定真正的错误在哪里。
注意:现在我已经修剪了结果变量,我得到了同样的错误。这是它的价值:
[{"ID":"1","NICK":"Jauuu","DESC":"Estic de proves amb php i mysql","FOTO":null,"DATA_NEIX":"1980-04-22","IDIOMA":null,"PAIS":"Espanya","GENERE":"H","ORIENTACIO":"heterosexu","ID_GRUP":null,"ESTAT":null,"ID_AMICS":null}]
临时解决方案和问题:
我已经解决了添加此行的问题:
sb.deleteCharAt(0);
这里:
...
sb.deleteCharAt(0);
String result = sb.toString();
result.trim();
...
我不知道为什么,但我的回复在我的StringBuilder的第0位添加了一个空字符(或类似的东西,因为修剪函数没有效果,但调试器也没有显示任何内容)。问题是异常消息令人困惑,并且在某种程度上说错了。
我在我的php文件中获得的响应是:
<?php
mysql_connect("127.0.0.1","root","");
mysql_select_db("prova");
$q=mysql_query("SELECT * FROM perfil WHERE ID='".$_REQUEST['id']."'");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(utf8_encode(json_encode($output)));
mysql_close();
?>
我现在已经解决了这个问题,但是有人知道为什么这个php会将这个空字符添加到我的响应中(它还会在最后添加它)?我是php的新手,所以也许这是一个愚蠢的问题,我只是想知道未来的情况。感谢所有帮助过您的评论的人。
答案 0 :(得分:2)
至少我有最终解决方案。
问题是我的php文件格式是带有BOM字符的utf-8。 BOM是给我带来问题的原因。我所要做的就是用我的Notepad ++打开文件并选择Codification-&gt; UTF-8而不用BOM并保存文件。
为了进一步简化,这里是我的Android代码的一个新的更短更简单的版本:
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2:8888/obtePerfil.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
//this line here is what saves a lot of work
String result = EntityUtils.toString(entity, HTTP.UTF_8);
// parse json data
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
TextView info = (TextView) findViewById(R.id.info);
String sInfo = "id: " + json_data.getInt("ID")
+ ", descripció: "
+ json_data.getString("DESC") + ", nick: "
+ json_data.getString("NICK")
+ ", data de naixement: "
+ json_data.getString("DATA_NEIX");
info.setText(sInfo);
}
} catch (Exception e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
} catch (Exception e) {
Log.e("log_tag", "Error in http connection "+ e.toString());
}
就是这样。