使用UrlConnection代替HttpClient和JSONArray

时间:2015-09-24 14:39:21

标签: android httpurlconnection

现在我正在使用不推荐使用的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吗?

2 个答案:

答案 0 :(得分:0)

你可以这样做:

 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

答案 1 :(得分:0)

@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