不支持DefaultHttpClient

时间:2016-07-17 07:01:55

标签: android android-studio

使用Marshmallow 6.0版本的API 23不支持DefaultHttpClient,而构建工具版本为:23.0.3

import java.io.UnsupportedEncodingException;
import java.util.List;

public class JSONParser {
    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";
    public JSONParser() {

    }
    public JSONObject makeHttpRequest(String url, String method, List<NameValuePair>params) {
        try {
            if(method == "POST") {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }else if(method == "GET") {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);
                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result" + e.toString());
        }
        try {
            jObj = new JSONObject(json);
        }
        catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data" + e.toString());
        }
        return jObj;
        }
    }

DefaultHttpClient上有剪切标记。 请告诉我解决问题的方法。

3 个答案:

答案 0 :(得分:0)

Android API级别23不再支持

HttpClient。您必须使用URLConnection。如果您想使用它,请将以下内容添加到build.gradle

android {
    useLibrary 'org.apache.http.legacy'
}

以下是公告: https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client

答案 1 :(得分:0)

这是因为DefaultHttpClient已被弃用。现在使用HttpUrlConnection

有关HttpUrlConnection

的详细信息,请访问此页面

示例:从网址

中检索数据
 URL url = new URL("http://www.android.com/");
   HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
   try {
     InputStream in = new BufferedInputStream(urlConnection.getInputStream());
     readStream(in);
   } finally {
     urlConnection.disconnect();
   } 

访问此处也是完整的GetPost方法示例使用HttpUrlConnection

https://www.numetriclabz.com/android-post-and-get-request-using-httpurlconnection/

答案 2 :(得分:0)

HttpClient不再受支持,知道您是否要使用它,您必须降级您的API级别,但如果我必须向您提供建议,您可以使用其他库,如VolleyRetrofit