什么是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
就像Blackbelt所言,HttpURLConnection是HTTPClient的默认替代品。 如果你在这里 (最后)喋喋不休,你可能会看到那些他们将在哪里集中资源。
不过,值得一提的是,一些常用的API正在被使用,如果你的应用程序的焦点不是网页浏览,而只是使用互联网来获取imgs,jsons,文本等等,那么它可以很好地工作。
我推荐Volley 。 它看起来会支持很长时间(根据我的意见),并支持谷歌本身。
如果您正在更新您的项目,并且您希望继续使用Apache HTTP API,则必须在build.gradle文件中声明以下内容:( 更多信息 ,请参见此处 )
android { useLibrary 'org.apache.http.legacy' }
注意你使用的gradle版本,我在我的gradle-wrapper.properties上使用2.6
distributionUrl=https\://services.gradle.org/distributions/gradle-2.6-all.zip
你是正确的DefaultHttpClient和AndroidHttpClient这两个网络类都被弃用。
现在,只有HttpUrlConnection是一个类将被用来替代它们。 Android开发者网站上的一些用法 。
“快乐编码… !!!”
经过3天的浏览和阅读有关HttpUrlConnection&CookieManager
我发现很多关于它的问题以及关于使用它发送Cookie的更多问题
所以我为它做了一个完整的解决方案:
处理曲奇:
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 ; }