使用 UrlConnection 而不是 HttpClient 和 JSONArray

Using UrlConnection instead of HttpClient with JSONArray

现在我正在使用已弃用的 HttpClient! :-( 所以我想使用 HttpUrlConnection.. 这是我的代码:

public JSONArray GetDetails(int ID){



        // Get HttpResponse Object from url.
        // Get HttpEntity from Http Response Object

        HttpEntity httpEntity = null;

        try
        {

           DefaultHttpClient httpClient = new DefaultHttpClient();  // Default HttpClient
            HttpGet httpGet = new HttpGet(StaticVariables.url_for_details + ID);

            HttpResponse httpResponse = httpClient.execute(httpGet);

            httpEntity = httpResponse.getEntity();


        } catch (ClientProtocolException e) {

            // Signals error in http protocol
            e.printStackTrace();

            //Log Errors Here



        } catch (IOException e) {
            e.printStackTrace();
        }


        // Convert HttpEntity into JSON Array
        JSONArray jsonArray = null;

        if (httpEntity != null) {
            try {
                String entityResponse = EntityUtils.toString(httpEntity);

                Log.e("Entity Response  : ", entityResponse);

                jsonArray = new JSONArray(entityResponse);

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

        return jsonArray;
    }

有人可以帮助我使用 HttpUrlConnection 而不是 HttpClient 以便我可以进一步使用 jsonarray 吗?

你可以这样做:

 public void getData(){

        try {
                  URL url = new URL("YOUR_URL");
                  HttpURLConnection con = (HttpURLConnection) url.openConnection();
                  String readStream = readStream(con.getInputStream());

                  System.out.println(readStream);

        JSONArray arr = new JSONArray(readStream);
        for (int i = 0; i < arr.length(); i++) {
            JSONObject object = arr.getJSONObject(i);
            String age=object.getString("age");
        }

                } catch (Exception e) {
                  e.printStackTrace();
                }


}
             private String readStream(InputStream in) {
                StringBuilder sb = new StringBuilder();
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(in));) {

                  String nextLine = "";
                  while ((nextLine = reader.readLine()) != null) {
                    sb.append(nextLine);
                  }
                } catch (IOException e) {
                  e.printStackTrace();
                }
                return sb.toString();
              }

更多参考请查看:

http://developer.android.com/reference/java/net/HttpURLConnection.html

@TargetApi(Build.VERSION_CODES.KITKAT)
    public static String getData(String uri) {
        BufferedReader reader = null;
        JSONArray jsonArray = null;

        try {
            URL url = new URL(uri);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            StringBuilder sb = new StringBuilder();
            reader = new BufferedReader(new InputStreamReader(con.getInputStream()));


            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            jsonArray = new JSONArray(sb);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String age = jsonObject.getString("age");
                Log.e("age", " is" + age);
            }

            return sb.toString();

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }
    }

这是我现在的方法,但是使用 JSONArray 将不起作用... 日志说: org.json.JSONException: 不是原始数组: class java.lang.StringBuilder