没有JSONArray的值

时间:2017-02-12 14:48:53

标签: arrays

JSONArray的问题。这是我的代码,帮帮我     这是我的JsonObject和错误..     Json错误:{"用户名":" rafyluc","记录":" 500"} {"用户名&# 34;:" inkinati""记录":" 600"} {"用户名":" rafyluc",& #34;记录":" 500"} {"用户名":" inkinati""记录":" 600& #34;}

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

1 个答案:

答案 0 :(得分:0)

查看代码,似乎JSONArray jArray = jsonObj.getJSONArray(jsonStr); 导致了问题。

jsonObj.getJSONArray要求传递的财产名称例如。 jsonObj.getJSONArray("array_property")但是,似乎您传递的是JSON字符串。

此外,在致电jsonObj.getJSONArray("array_property")之前,您需要确保array_property

中存在jsonObj