php响应时出现JSON错误

时间:2013-03-06 13:31:18

标签: java android json arrays jsonexception

我遇到了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)
有人可以帮帮我吗?提前谢谢。

1 个答案:

答案 0 :(得分:1)

你在这里扯了你的索引:

for(int k=0;i<gL2.length();k++){
 ------------^

您应该使用k,而不是i。另外,您可能会从代码中收集到大量重复代码(例如,重复调用mPrefs = getSharedPreferences(PREFS, 0);