Pregunta

I have a problem with JSON.

I've got this service, that connect to php and send me data in json format. All was ok (months ago), but today I modified a little thing and don't work.

I add a call function to simply compare 2 jSon arrays and to create another one with the elements that are in one but no in other.

this is the code of service:

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();
    }
    }

And this is my new 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)

somebody could help me? Thank You in advance.

¿Fue útil?

Solución

You flubbed your index check here:

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

You should be using k, not i. On a side note, you have alot of repetitive code that you could probably glean out of your code (eg, the repeated calls to mPrefs = getSharedPreferences(PREFS, 0);.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top