我有JSON的問題。當php響應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)
有人能幫助我嗎?先謝謝你。
你的數組比較實現是天真的。它的複雜度是o(n²)。 groupid(o(log(n))上的兩個數組,允許您進行ao(n)比較(如果g1 [i] == g2 [k] => add; else if g1 [i] < g2[k] => i ++; else如果g1 [i]> g2 [k] => k ++;) – njzk2 2013-03-06 14:02:25
謝謝你@ njzk2。你說得對,我會在有時間之後做出來的,謝謝 – carlosdiazp 2013-03-06 17:56:05