2014-04-26 37 views
0

昨天它工作正常,今天我得到這樣的消息:java.lang.String類型的值不能轉換爲JSONArray ....問題接受

這是

private class HttpAsyncTaskFamilies extends AsyncTask<String, Void, String> { 

@Override 
protected String doInBackground(String... urls) { 
    return GET(urls[0]); 
} 

// onPostExecute displays the results of the AsyncTask 
@Override 
protected void onPostExecute(String result) { 
    JSONArray jArray; 
    String families = ""; 
    Log.d(TAG, "result in onPostExectute Families -> " + result); 
    try { 
     DatabaseHelper helper = new DatabaseHelper(
       getApplicationContext()); 

     try { 
      helper.dropAndCreate(helper.getConnectionSource(), 
        Familia.class); 

     // Do the inserts in table Familia 
       jArray = new JSONArray(result); 

       Log.d(TAG, "jsonArray -> " + jArray); 
       for (int i = 0; i < jArray.length(); i++) { 
        JSONObject json = jArray.getJSONObject(i); 
        Log.d(TAG, "jsonObject -> " + json.toString()); 

        String id = json.getString("familia_id"); 
        Log.d(TAG, "id -> " + id); 
        int id_family = Integer.parseInt(json 
          .getString("familia_id")); 

        // for each Familia 
        Familia familia = new Familia(json.getInt("familia_id"), 
          json.getString("nom_familia")); 
        helper.getFamiliaDao().create(familia); 
        families += json.getString("nom_familia"); 
        Log.d(TAG, "Familia inserida -> " + familia); 
       } 
       Log.d(TAG, "families actualitzades! " + families); 

      } catch (SQLException e) { 
       e.printStackTrace(); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

    } 
} 

的錯誤是在行:那我收到的JSON:當我嘗試從做一個JSONArray

enter image description here

和代碼將停止: jArray =新的JSONArray(結果);

enter image description here

任何想法?這是奇怪的是,昨天工作正常,今天我只能有這個錯誤...

找不到方法Add:

enter image description here

更多信息:

在onCreate方法我有:

new HttpAsyncTaskFamilies().execute(「http://aplicacionesmovilandroid.es/json/families.php」);

然後當HttpAsyncTaskFamilies()調用GET方法,這是一個方法:

public String GET(String url) { 
InputStream inputStream = null; 
String result = ""; 
try { 
    // create HttpClient 
    HttpClient httpClient = new DefaultHttpClient(); 

    UtilDAOImpl dao = new UtilDAOImpl(this); 

     Versio versioObject = dao.lookupVersioCentre("centres"); 
     String versio = versioObject.getVersio(); 

     // we change the white space for %20, a representation of the white 
     // space 
     // that php can recognise. Without it, the php file always deceive a 
     // null value 
     // instead of the actual version 
     // String finalVersion = versio.replace(" ", "%20"); 

     // make GET request to the given URL 
     // HttpResponse httpResponse = httpClient.execute(new HttpGet(url 
     // + "?centres_versio=" + finalVersion)); 

     HttpResponse httpResponse = httpClient.execute(new HttpGet(url)); 

     // receive response as InputStream 
     inputStream = httpResponse.getEntity().getContent(); 

     // convert InputStream to string 
     if (inputStream != null) { 
      result = convertInputStreamToString(inputStream); 
     } else { 
      result = "No ha funcionat!"; 
     } 
    } catch (Exception e) { 
     Log.d("InputStream", e.getLocalizedMessage()); 
    } 
    return result; 
} 
+1

郵政編碼不是照片。 –

+0

你如何獲得'result'的值?在開始時可能是否有一些非打印字符? – Henry

+0

@Chris查看我的回答 – Akshay

回答

0

得到響應後,你必須result轉化爲JSONObject如..

JSONObject jsonObj = new JSONObject(result); 
FamiliesArray = jsonObj.getJSONArray(Families); 

可以和我看到你錯過了..

+0

那麼,該值實際上是一個數組而不是一個對象。所以你的建議無效。 – Henry

+0

但我在json中的數組內部接收3個對象...首先,我需要先執行一個JSONArray然後一個循環,然後在該循​​環內部執行JSONObjects,對不對?就像我在郵件中添加的代碼一樣。 – Chris

-1

你的代碼沒有錯,但是 這些行可能會產生問題 還是沒法確定!

評論這些行!

Log.d(TAG,"arrayJson ->"+arrayJson.toString()); 
+0

是的,但我想看看它正在創建的JSONArray ...這是沒有,因爲應用程序崩潰時試圖創建JSONArray ... :-( – Chris

+0

Log.d(標記,「jsonArray - >」+ jArray) ;現在檢查這些行,因爲這行不會打印在您的日誌。 – sandy

+0

嘿,只是檢查你的JSON在這個鏈接http://jsonformatter.curiousconcept.com/#jsonformatter,因爲對我來說你的JSON格式它顯示無效 – sandy

0

替換你的代碼,

Familia familia = new Familia(json.getInt("familia_id"), 
         json.getString("nom_familia")); 

Familia familia = new Familia(Integer.parseInt(json.getString("familia_id")), 
         json.getString("nom_familia")); 

因爲服務器發送familia_id是String形式。希望這項工作...... :)

+0

是的,我很抱歉。我只是在嘗試......這是爲了做一個測試。但我也嘗試過使用getString ...也不工作:-( – Chris

+0

您正在檢查的Android版本 –

+0

我的真實設備是API 10 – Chris