茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1729816 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_0167.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

爪哇咖啡屋 : [轉貼]關於 DefaultHttpClient 過時的問題

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15773
[轉貼]關於 DefaultHttpClient 過時的問題

關於DefaultHttpClient 過時的問題


今天在使用之前的一個demo的時候 在導入studio之後發現 缺少jar包 然後在導入jar包後,有些方法依然不能正常使用,後來搜索了一下,恍然大悟,原來是某些api過時導致的 ,

決絕方法也 很簡單 最簡單的方法就是 直接將studio 裡面的Api版本 修改成以前的版本 高版本是可以兼容低版本的 ,


修改build.Gradle 文件裡面的 相關引用



android {
compileSdkVersion 18

defaultConfig {
8
17
}

這樣就可以輕鬆搞定了

當然這部是最終決絕的辦法 我們要瞭解 新的知識 ,

下面是從網絡上轉載的一遍介紹的比較詳細的文章 供大家學習吧


原文地址: http://www.yeetrack.com/?p=760

最近在使用Apache的httpclient的時候,maven引用了最新版本4.3,發現Idea提示DefaultHttpClient等常用的類已經不推薦使用了,之前在使用4.2.3版本的時候,還沒有被deprecated。去看了下官方文檔,確實不推薦使用了, 點擊此處詳情

  • DefaultHttpClient —> CloseableHttpClient
  • HttpResponse —> CloseableHttpResponse

官方給出了新api的樣例,如下。

Get方法:


 CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://targethost/homepage");
CloseableHttpResponse response1 = httpclient.execute(httpGet);
// The underlying HTTP connection is still held by the response object
// to allow the response content to be streamed directly from the network socket.
// In order to ensure correct deallocation of system resources
// the user MUST either fully consume the response content or abort request
// execution by calling CloseableHttpResponse#close().
//建立的http連接,仍舊被response1保持著,允許我們從網絡socket中獲取返回的數據
//為了釋放資源,我們必須手動消耗掉response1或者取消連接(使用CloseableHttpResponse類的close方法)
try {
System.out.println(response1.getStatusLine());
HttpEntity entity1 = response1.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
EntityUtils.consume(entity1);
} finally {
response1.close();
}

Post方法:


 HttpPost httpPost = new HttpPost("http://targethost/login");
//拼接參數
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("username", "vip"));
nvps.add(new BasicNameValuePair("password", "secret"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response2 = httpclient.execute(httpPost);
try {
System.out.println(response2.getStatusLine());
HttpEntity entity2 = response2.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
//消耗掉response
EntityUtils.consume(entity2);
} finally {
response2.close();
}

再往下看HttpClients的源碼,具體的實現都在 HttpClientBuilderbuild方法中,有興趣的可以去apache看源碼。


 /**
* Creates {@link CloseableHttpClient} instance with default
* configuration.
*/
public static CloseableHttpClient createDefault() {
return HttpClientBuilder.create().build();
}


原文出處: 关于DefaultHttpClient 过时的问题 - q9104422999的博客 - CSDN博客
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15773
[轉貼]HttpClient 請求 DefaultHttpClient 過時替換

HttpClient請求DefaultHttpClient過時替換


package com.huanlv.util;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;




import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;


import net.sf.json.JSONObject;


/**
*
* @author david
*
*/
@SuppressWarnings("deprecation")
public class HttpClientUtil {

//
public static final String learnSelUrl="http://192.168.0.124:8080/site/trdcredit/jxlxxwactivereq";

/**
* post請求
* @param url
* @param param
* @return
* @throws IOException
*/
public static JSONObject postJsonData(String url,Map<String,String> params){
CloseableHttpClient httpclient = HttpClientUtil.createDefault();
HttpPost httpPost = new HttpPost(url);
//拼接參數
List<NameValuePair> list = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : params.entrySet()) {
String key = entry.getKey().toString();
String value = entry.getValue().toString();

System.out.println("key=" + key + " value=" + value);
NameValuePair pair = new BasicNameValuePair(key, value);
list.add(pair);
}
CloseableHttpResponse response=null;
try {
httpPost.setEntity(new UrlEncodedFormEntity(list));
response = httpclient.execute(httpPost);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/**請求發送成功,並得到響應**/
JSONObject jsonObject=null;
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
HttpEntity httpEntity = response.getEntity();
String result=null;
try {

result = EntityUtils.toString(httpEntity);
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}// 返回json格式:
jsonObject = JSONObject.fromObject(result);
}
return jsonObject;
}

/**
* Creates {@link CloseableHttpClient} instance with default
* configuration.
*/
public static CloseableHttpClient createDefault() {
return HttpClientBuilder.create().build();
}

/**
* http發送post請求
* @param url
* @param maps
* @return
*/
@SuppressWarnings({ "resource" })

public static JSONObject sendPost(String url,Map<String,String> params){
DefaultHttpClient client = new DefaultHttpClient();
/**NameValuePair是傳送給服務器的請求參數 param.get("name") **/
List<NameValuePair> list = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : params.entrySet()) {
String key = entry.getKey().toString();
String value = entry.getValue().toString();
System.out.println("key=" + key + " value=" + value);
NameValuePair pair = new BasicNameValuePair(key, value);

list.add(pair);
}
UrlEncodedFormEntity entity=null;
try {
/**設置編碼 **/
entity = new UrlEncodedFormEntity(list,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
/**新建一個post請求**/
HttpPost post = new HttpPost(url);
post.setEntity(entity);
HttpResponse response=null;
try {
/**客服端向服務器發送請求**/
response = client.execute(post);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/**請求發送成功,並得到響應**/
JSONObject jsonObject=null;
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
HttpEntity httpEntity = response.getEntity();
String result=null;
try {
result = EntityUtils.toString(httpEntity);
} catch (ParseException e) {
e.printStackTrace();

} catch (IOException e) {
e.printStackTrace();
}// 返回json格式:
jsonObject = JSONObject.fromObject(result);
}
return jsonObject;
}
}

原文出處: HttpClient请求DefaultHttpClient过时替换 - u010849331的专栏 - CSDN博客
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15773
[轉貼]Why I am getting DefaultHttpClient is deprecated?
Why I am getting DefaultHttpClient is deprecated?

I am working on a project in which I need to make a call to my service and my service will return the data back in JSON format. And I don't need to serialize this JSON response to any POJO, I just need to get the data back as String. And this application is very performance critical so HttpClient has to be pretty fast
So I decided to use Apache HttpClient or is there any better alternative which I can use?
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpUriRequest request = new HttpGet("some-url");
request.addHeader("Context", "some-value");
HttpResponse response = httpClient.execute(request);
String response =  IOUtils.toString(response.getEntity().getContent(), "UTF-8");

But it complains that The type DefaultHttpClient is deprecated so maybe they have new version of HttpClient or some other way of making a HttpClient call to an URL?
Is there anything I am missing?



Use this:
HttpClient httpclient = HttpClientBuilder.create().build();

Refer to this stackoverflow post



From Apache HTTP Client API version 4.3 on wards, DefaultHttpClient is deprecated.
Use following maven dependency as an example.
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.1</version>
</dependency>

Following import.
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGett;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.client.methods.HttpUriRequest;

Following code block.
HttpClient client = HttpClientBuilder.create().build();
HttpUriRequest httpUriRequest = new
HttpGet("http://example.domain/someuri");

HttpResponse response = client.execute(httpUriRequest);
System.out.println("Response:"+response);


原文出處:java - Why I am getting DefaultHttpClient is deprecated? - Stack Overflow
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|