我对 JSON 有疑问。
我有这个服务,它连接到 php 并以 json 格式向我发送数据。一切都很好(几个月前),但是今天我修改了一些东西并且不起作用。
我添加了一个调用函数来简单地比较 2 个 jSon 数组并创建另一个数组,其中的元素在一个数组中但在另一个数组中没有。
这是服务代码:
public class GroupsTaskAlarmChecker extends Service implements Runnable {
public void run() {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("role", role));
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://blablabla.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
noInternet=false;
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
noInternet=true;
}
//convert response to string
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());
noInternet=true;
}
jArray2= result;
mPrefs = getSharedPreferences(PREFS, 0);
if (jArray2.equals(groupList) || noInternet) {
handler.sendEmptyMessage(NO_CHANGE);
}
else {
if (!noInternet)
{
try {
prepareForNewImage(jArray2);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
Editor e = mPrefs.edit();
e.putString("groupList", jArray2);
e.commit();
handler.sendEmptyMessage(THERE_ARE_CHANGE);
}
}
public void prepareForNewImage(String j) throws JSONException {
String gL = mPrefs.getString("groupList", null);
Log.e("string j",j);
String jA = "";
jA=j;
if(!gL.equals(null)) {
JSONArray gL2 = new JSONArray(gL);
JSONArray jA2 = new JSONArray(jA);
boolean is = false;
for(int i=0;i<jA2.length();i++){
JSONObject json_data2 = jA2.getJSONObject(i);
String gId_jA2=json_data2.getString("groupId");
for(int k=0;i<gL2.length();k++){
JSONObject json_data = gL2.getJSONObject(k);
String gId_gL2=json_data.getString("groupId");
if (gId_gL2.equals(gId_jA2))
is=true;
}
if (!is)
{
is=false;
newGroups.put(json_data2);
}
Log.i("loop","end of first loop");
}
Editor e = mPrefs.edit();
e.putString("newGroups", newGroups.toString());
e.commit();
}
}
这是我的新 logCat:
03-06 15:44:54.243: W/System.err(19868): org.json.JSONException: End of input at character 0 of
03-06 15:44:54.253: W/System.err(19868): at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
03-06 15:44:54.253: W/System.err(19868): at org.json.JSONTokener.nextValue(JSONTokener.java:97)
03-06 15:44:54.253: W/System.err(19868): at org.json.JSONArray.<init> (JSONArray.java:87)
03-06 15:44:54.253: W/System.err(19868): at org.json.JSONArray.<init>(JSONArray.java:103)
03-06 15:44:54.253: W/System.err(19868): at com.org.tfc_android.GroupsTaskAlarmChecker.prepareForNewImage(GroupsTaskAlarmChecker.java:162)
03-06 15:44:54.253: W/System.err(19868): at com.org.tfc_android.GroupsTaskAlarmChecker.run(GroupsTaskAlarmChecker.java:139)
03-06 15:44:54.253: W/System.err(19868): at java.lang.Thread.run(Thread.java:856)
有人可以帮助我吗?先感谢您。