即时通讯尝试解析使用android发送php的json数据。这是代码。 JSON的输出看起来是这样的: “{文件:[{IDNumber中: '28044684',DOCTYPE: '身份证'}],成功:1}”JSON字符串解析
即时得到错误ERROR parsing JSONObject value [null];
甚至当我直接分配json输出到变量json。可能是什么问题呢??
String json = "";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("idnumber", parameter));
// Making HTTP request
try {
// check for request method
if (method == "POST") {
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} else if (method == "GET") {
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 300);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line=reader.readLine())!= null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (Exception e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
try {
jObj = new JSONObject(json.substring(json.indexOf("{"),
json.lastIndexOf("}") + 1));
} catch (Exception e0) {
Log.e("JSON Parser0",
"Error parsing data [" + e0.getMessage() + "] "
+ json);
Log.e("JSON Parser0", "Error parsing data " + e0.toString());
try {
jObj = new JSONObject(json.substring(1));
} catch (Exception e1) {
Log.e("JSON Parser1",
"Error parsing data [" + e1.getMessage() + "] "
+ json);
Log.e("JSON Parser1",
"Error parsing data " + e1.toString());
try {
jObj = new JSONObject(json.substring(2));
} catch (Exception e2) {
Log.e("JSON Parser2",
"Error parsing data [" + e2.getMessage()
+ "] " + json);
Log.e("JSON Parser2",
"Error parsing data " + e2.toString());
try {
jObj = new JSONObject(json.substring(3));
} catch (Exception e3) {
Log.e("JSON Parser3", "Error parsing data ["
+ e3.getMessage() + "] " + json);
Log.e("JSON Parser3", "Error parsing data "
+ e3.toString());
}
}
}
}
}
return null;
json解析是非常简单,但不是你做了什么。尝试一些教程 – 2013-05-03 10:20:43
你必须更加具体,它可以是任何东西,即使是“返回null”。尝试将其缩减为可能的最简单示例,使用硬编码字符串并且不存在回退。 – Szocske 2013-05-03 10:22:50