Android API 23删除了包

时间:2015-08-24 10:51:29

标签: android apache http

org.apache.http的最佳替代品是什么?

因为他们在Android API差异报告中这样说。

在API 23中删除了包

org.apache.commons.logging   
org.apache.http  
org.apache.http.auth     
org.apache.http.auth.params  
org.apache.http.client   
org.apache.http.client.entity    
org.apache.http.client.methods   
org.apache.http.client.params    
org.apache.http.client.protocol  
org.apache.http.client.utils     
org.apache.http.conn.params  
org.apache.http.conn.routing     
org.apache.http.conn.util    
org.apache.http.cookie   
org.apache.http.cookie.params    
org.apache.http.entity   
org.apache.http.impl     
org.apache.http.impl.auth    
org.apache.http.impl.client  
org.apache.http.impl.conn    
org.apache.http.impl.conn.tsccm  
org.apache.http.impl.cookie  
org.apache.http.impl.entity  
org.apache.http.impl.io  
org.apache.http.io   
org.apache.http.message  
org.apache.http.protocol     
org.apache.http.util

3 个答案:

答案 0 :(得分:1)

你是对的DefaultHttpClient和AndroidHttpClient都弃用了这两个网络类。

现在,只有HttpUrlConnection类才会被用作替代它们。 Android开发者网站上的一些usage

"快乐编码...... !!!"

答案 1 :(得分:1)

与Blackbelt一样,HttpURLConnection是HTTPClient的默认替代品。如果你感谢here(最后),你可能会发现他们将把资源集中在那里。

但是,值得一提的是,正在使用一些常见的API,并且如果您的应用的重点不是网页浏览,而是仅使用互联网来获取imgs,jsons,文本等,那么效果很好。

我建议Volley。看起来好像它会被支持很长时间(基于我的观点),并得到谷歌本身的支持。

答案 2 :(得分:0)

经过3天的搜索和阅读HttpUrlConnection& CookieManager

我发现很多关于它的问题以及关于用它发送Cookies的更多问题

所以我为它做了一个完整的解决方案:

for Handle Cookies:

static CookieManager myCookies = new CookieManager(null, CookiePolicy.ACCEPT_ALL);

final public static void saveCookies(HttpURLConnection connection , Context context) {
Map<String, List<String>> headerFields = connection.getHeaderFields();

List<String> cookiesHeader = null;
try {
    cookiesHeader = headerFields.get("Set-Cookie");
} catch (Exception e) {
    e.printStackTrace();
}

if (cookiesHeader != null && myCookies != null) {
    for (String cookie : cookiesHeader) {
        try {
            cookie = cookie.replace("\"", "");
            myCookies.getCookieStore().add(connection.getURL().toURI(), HttpCookie.parse(cookie).get(0));
            String new_cookie = TextUtils.join(";", myCookies.getCookieStore().getCookies());

            PreferenceManager.getDefaultSharedPreferences(context).edit().putString("cookie", new_cookie).commit();

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

final public static void loadCookies(HttpURLConnection connection , Context context) {
if (myCookies != null && myCookies.getCookieStore().getCookies().size() > 0) {
    connection.setRequestProperty("Cookie", TextUtils.join(";", myCookies.getCookieStore().getCookies()));
}
else {
    String new_cookie = PreferenceManager.getDefaultSharedPreferences(context).getString("cookie" , "");
    connection.setRequestProperty("Cookie", new_cookie );
}
}

对于使用Json数据的发布请求

  public String  URL_Connectin_Post ( String  endPoint , JSONObject data , Context context )
{
    URL url;
    try {
        url = new URL(baseUrl + endPoint);

        urlConnection = (HttpURLConnection) url.openConnection();
        loadCookies(urlConnection , context);
        urlConnection.setReadTimeout(15000);
        urlConnection.setConnectTimeout(15000);
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);

        /* optional request header */
        urlConnection.setRequestProperty("Content-Type", "application/json");
        urlConnection.setRequestProperty("charset", "UTF-8");
        urlConnection.addRequestProperty("Accept-Encoding", "UTF-8");

        /* optional request header  with UTF-8*/
        urlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

        /*  use this when you know data length  */
        urlConnection.setFixedLengthStreamingMode(data.toString().getBytes("UTF-8").length);

        /*  use this when you dont  know data length  */
  //      urlConnection.setChunkedStreamingMode(100);

        urlConnection.setUseCaches(true);
        urlConnection.setRequestMethod("POST");
        urlConnection.connect();

        OutputStream os = urlConnection.getOutputStream();
        BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(os, "UTF-8"));
        writer.write(data.toString());
        writer.flush();
        writer.close();
        os.close();

        int responseCode=urlConnection.getResponseCode();
        if (responseCode == HttpsURLConnection.HTTP_OK)
        {
            InputStream in = urlConnection.getInputStream();
            saveCookies(urlConnection , context);
            Result = convertStreamToString(in);

        }

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

    return Result;
}

获取请求

    public  String URL_Connectin_Get(String  endPoint , Context context)
{
    URL url;
    try {
        url = new URL(baseUrl + endPoint);

        urlConnection = (HttpURLConnection) url.openConnection();
        loadCookies(urlConnection , context);

        urlConnection.setRequestProperty("Content-Type", "application/json");
        urlConnection.setRequestProperty("charset", "UTF-8");
        urlConnection.addRequestProperty("Accept-Encoding", "UTF-8");
        urlConnection.setDoInput(true);
        urlConnection.setRequestMethod("GET");
        urlConnection.connect();

        int responseCode=urlConnection.getResponseCode();
        if (responseCode == HttpsURLConnection.HTTP_OK) {

            InputStream in = urlConnection.getInputStream();
            if (urlConnection.getContentEncoding() != null && urlConnection.getContentEncoding().contains("gzip")) {
                GZIPInputStream inn = new GZIPInputStream(in);
                saveCookies(urlConnection , context);
            } else {
                saveCookies(urlConnection  , context);
            }
            Result = convertStreamToString(in);
        }

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

    return Result ;
}