2017-02-12 73 views
0

JSONArray存在問題。這是我的代碼,幫我 這是我的JsonObject和錯誤.. Json errore:{「Username」:「rafyluc」,「Record」:「500」} {「Username」:「inkinati」,「實錄 「:」 600 「} {」 用戶名 「:」 rafyluc」, 「記錄」: 「500」} { 「用戶名」: 「inkinati」, 「記錄」: 「600」}JSONArray沒有值

public class ListaAlunni extends AppCompatActivity { 
    private static ArrayList<Alunni> alunni; 
    AlunniListAdapter customAdapter; 
    private String TAG = ListaAlunni.class.getSimpleName(); 
    private static String url = "http://192.168.1.11:80/webservice/lista.php"; 

    ArrayList<HashMap<String, String>> itemList; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.lista_alunni); 
     getSupportActionBar().setTitle("Lista Alunni"); 
     alunni = new ArrayList<Alunni>(10); 
     popola(); 
    } 

    private void setTextLista() { 
     ListView ll = (ListView) findViewById(R.id.lista); 
     ll.setAdapter(new AlunniListAdapter(ListaAlunni.this, R.layout.lista_row, alunni)); 
    } 

    private void popola() { 
     new AsyncTask<Object, Object, Object>() { 

      @Override 
      protected void onPreExecute() { 
       alunni = new ArrayList<Alunni>(10); 
      } 

      @Override 
      protected Object doInBackground(Object... params) { 
       HttpHandler sh = new HttpHandler(); 
       String jsonStr = sh.makeServiceCall(url); 
       if (jsonStr != null) { 
try { 
         JSONObject jsonObj = new JSONObject(jsonStr); 
         Log.d("Debug_object", String.valueOf(jsonObj)); 

         // Getting JSON Array node 
         // try { 
         //JSONArray jArray=new JSONArray(jsonStr); 
         JSONArray jArray = jsonObj.getJSONArray(jsonStr); 
         Log.d("DEBUG_json", String.valueOf(jArray)); 
         for (int i = 0; i < jArray.length(); i++) { 
          JSONObject json_data = jArray.getJSONObject(i); 
          Log.i("TEST", "Username: " + json_data.getString("Username") + 
            ", record: " + json_data.getString("Record") 

          ); 
          String nome= json_data.getString("Username"); 
          String record=json_data.getString("Record"); 
          // alunni.add(new Alunni(nome,record)); 

          HashMap<String, String> item = new HashMap<>(); 
          item.put("Username", nome); 
          item.put("Record", record); 

          // adding item to item list 
          itemList.add(item); 
         } 

        } catch (final JSONException e) { 
         Log.e(TAG, "Json errore: " + e.getMessage()); 
runOnUiThread(new Runnable() { 
          @Override 
          public void run() { 
           Toast.makeText(getApplicationContext(), 
             "Json errore: " + e.getMessage(), 
             Toast.LENGTH_LONG) 
             .show(); 
          } 
         }); 

        } 
       } else { 
        Log.e(TAG, "Couldn't get json from server."); 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          Toast.makeText(getApplicationContext(), 
            "Couldn't get json from server. Check LogCat for possible errors!", 
            Toast.LENGTH_LONG) 
            .show(); 
         } 
        }); 

       } 

       return null; 
      } 

@Override 
      protected void onPostExecute(Object o) { 
       setTextLista(); 

      } 
     }.execute(); 
    } 

} 


classe HttpHandler: 

public class HttpHandler { 

    private static final String TAG = HttpHandler.class.getSimpleName(); 

    public HttpHandler() { 
    } 

    public String makeServiceCall(String reqUrl) { 
     String response = null; 
     try { 
      URL url = new URL(reqUrl); 
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
      conn.setRequestMethod("POST"); 
      // read the response 
      InputStream in = new BufferedInputStream(conn.getInputStream()); 
      response = convertStreamToString(in); 
     } catch (MalformedURLException e) { 
Log.e(TAG, "MalformedURLException: " + e.getMessage()); 
     } catch (ProtocolException e) { 
      Log.e(TAG, "ProtocolException: " + e.getMessage()); 
     } catch (IOException e) { 
      Log.e(TAG, "IOException: " + e.getMessage()); 
     } catch (Exception e) { 
      Log.e(TAG, "Exception: " + e.getMessage()); 
     } 
     return response; 
    } 

    private String convertStreamToString(InputStream is) { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
     StringBuilder sb = new StringBuilder(); 

     String line; 
     try { 
      while ((line = reader.readLine()) != null) { 
       sb.append(line).append('\n'); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       is.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return sb.toString(); 
    } 
} 
+0

可以添加例外的完整堆棧跟蹤? –

回答

0

看着你的代碼,看起來JSONArray jArray = jsonObj.getJSONArray(jsonStr); 是造成這個問題。

jsonObj.getJSONArray要求通過的財產的名稱例如相反,它似乎是傳遞JSON字符串。

此外,調用jsonObj.getJSONArray("array_property")之前,你需要確保的是,array_property存在於jsonObj

+0

什麼「array_property」被傳遞?以我的問題爲例 –

+0

假設,** jsonStr = {「name」:「John Doe」,「age」:21,「interests」:[「Reading」,「Swimming」,「Soccer」]} ** 如果'jsonObj'的'interest'屬性是一個數組,那麼只能執行'jsonObj.getJSONArray(「interest」)'。 你可以發佈'jsonStr'的實際值嗎? –

+0

D/Debug_jsonStr:{「Username」:「rafyluc」,「Record」:「500」} {「Username」:「inkinati」,「Record」:「600」} {「Username」:「rafyluc」,「Record 「:」500「} {」Username「:」inkinati「,」Record「:」600「} –