我已将数据插入名为tsk
的服务器表中。现在我想在我的Android代码中使用JsonArray
检索它。我使用了以下php code
,我可以在网页上看到解析后的数组。问题是我在错误日志中收到错误04-04 01:36:18.198: E/log_tag(2254): Error parsing data org.json.JSONException: Value of type java.lang.String cannot be converted to JSONArray
。
我的数据库字段如下:
tid
是Int
设置为primary key
和autoincremented
。
stat
是Text
,字段长度设置为100。
r
是VarChar
,长度设置为100。
我在Android中的Json检索代码如下:
String result = null;
InputStream is = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("myurl");
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success ");
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection "+e.toString());
Toast.makeText(getActivity(), "Connection fail", Toast.LENGTH_SHORT).show();
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}
catch(Exception e)
{
Log.e("log_tag", "Error converting result "+e.toString());
Toast.makeText(getActivity(), " Input reading fail", Toast.LENGTH_SHORT).show();
}
try
{
JSONArray jArray = new JSONArray(result);
String s="",s1,s2,s3,s4,s5;
Log.w("Lengh",""+jArray.length());
for(int i=0;i<jArray.length();i++){
// final ListView lview=(ListView) rootView.findViewById(R.id.listViewmytask);
JSONObject json_data = jArray.getJSONObject(i);
s=json_data.getString("stat");
Log.i("taskid",""+s);
}
}
catch(JSONException e)
{
Log.e("log_tag", "Error parsing data "+e.toString());
Toast.makeText(getActivity(), "JsonArray fail", Toast.LENGTH_SHORT).show();
}
JSON reesponse:
[{"tid":"9","head":"task2","des":"task2.2","dateone":"2014\/4\/4 ","datetwo":"","timeone":"12:18","timetwo":"13:18","r":"123","s":"123456","stat":"not defined"},
{"tid":"8","head":"task1","des":"task1.1","dateone":"2014\/4\/4 ","datetwo":"","timeone":"12:11","timetwo":"13:11","r":"12345","s":"123456","stat":"not defined"},
{"tid":"10","head":"task3","des":"task3.3","dateone":"2014\/4\/3 ","datetwo":"2014\/5\/3 ","timeone":"13:25","timetwo":"13:26","r":"456","s":"123456","stat":"not defined"}, {"tid":"11","head":"task4","des":"task4.4","dateone":"2014\/3\/31 ","datetwo":"2014\/4\/30 ","timeone":"13:30","timetwo":"14:30","r":"123456","s":"123456","stat":"not defined"}]
String Builder result
显示以下值:但它有一些未定义的字符,如i`?(倒置问号)。
04-04 01:44:04.965: I/result(2406): i`?[{"tid":"10","head":"task3","des":"task3.3","dateone":"2014-4-3 ","datetwo":"2014-5-3 ","timeone":"13:25","timetwo":"13:26","r":"456","s":"123456","stat":"not defined"}]
答案 0 :(得分:1)
可能是因为JSON响应格式不正确。您可以发布您的JSON响应以更好地理解它吗?您可以在此网址中验证您的JSON响应 - http://jsonlint.com/
您可以使用这个简单的JSON解析器来解析您的JSON响应 - https://json-simple.googlecode.com/files/json-simple-1.1.1.jar。
答案 1 :(得分:1)
似乎服务器没有返回jsonArray,确保第一个响应字母,如果它启动{应该是json对象而且如果服务器只返回'[]'这个那么服务器返回空数据,这不可能是解析成json数组
答案 2 :(得分:1)
更改此代码并尝试
StringBuilder sb = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, HTTP.UTF_8));
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} finally {
is.close();
reader.close();
}
result=sb.toString();