Android 在JSONArray中使用UrlConnection而不是HttpClient

Android 在JSONArray中使用UrlConnection而不是HttpClient,android,httpurlconnection,Android,Httpurlconnection,现在我正在使用已弃用的HttpClient!:-( 所以我想使用HttpUrlConnection。。 这是我的密码: public JSONArray GetDetails(int ID){ // Get HttpResponse Object from url. // Get HttpEntity from Http Response Object HttpEntity httpEntity = null; try

现在我正在使用已弃用的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();
              }
public void getData(){
试一试{
URL=新URL(“您的URL”);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
字符串readStream=readStream(con.getInputStream());
System.out.println(readStream);
JSONArray arr=新的JSONArray(readStream);
对于(int i=0;i
有关更多参考信息,请检查以下内容:

@TargetApi(Build.VERSION\u CODES.KITKAT)
公共静态字符串getData(字符串uri){
BufferedReader reader=null;
JSONArray JSONArray=null;
试一试{
URL=新的URL(uri);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
StringBuilder sb=新的StringBuilder();
reader=newbufferedReader(newInputStreamReader(con.getInputStream());
弦线;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
jsonArray=新jsonArray(sb);
for(int i=0;i
这是我现在使用的方法,但是使用JSONArray的方法不起作用。。。 日志上写着:
org.json.JSONException:不是一个原始数组:class java.lang.StringBuilder

您面临的问题是什么?或者您特别不了解/不知道什么阻止了您使用HttpUrlConnection?我不知道我必须以何种方式在代码中使用HttpUrlConnection OK现在我有来自internet的所有数据,但现在我想单个项目如:jsonArray.get(“name”)和你成为唯一的名称你的json是什么,发布在这里[{“uid”:“112”,“unique_id”:“55f44cbe8899e0.90440511”,“name”:“aa”,“全名”:“tt”,“email”:aa@gmail.com,“年龄”:“18”,“关于客户”:“o”,“加密密码”:“+bla”,“salt”:“bla”,“创建时间”:“2015-09-12 18:03:10”,“更新时间”:null,“imageName”:“bla.jpg”}]例如,我只想在文本视图或其他东西中显示年龄,你可以看看我的答案吗?我不能对此进行评论,因为代码中的字符比我在这里可以使用的多。你不是在使用我在这里发布的答案吗?顺便说一句,你需要在代码中将字符串生成器转换为字符串
@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;
                }
            }
        }
    }