2017-07-28 59 views
0

IM deveolping防盜應用,發現了這個連接類,但也有一些不用的東西:httppparams,basicHttpParams,httpconnectionparams,defaulthttpclient棄用

這裏deperecated線: httppparams,basicHttpParams,httpconnectionparams,defaulthttpclient 哪有我解決這個問題 ?? 這裏是過時的線的截圖:

screenshot

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

import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.BasicResponseHandler; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.apache.http.params.BasicHttpParams; 
import org.apache.http.params.HttpConnectionParams; 
import org.apache.http.params.HttpParams; 
import org.apache.http.params.HttpProtocolParams; 

public class HttpRmi { 

private HttpPost request = null; 
private List<NameValuePair> nameValuePairs = null; 
private int connectionTimeoutMillis = 300*1000; 
private int socketTimeoutMillis = 0; 
private HttpClient httpClient; 

public HttpRmi(String url){ 
    request = new HttpPost(url); 
    nameValuePairs = new ArrayList<NameValuePair>(1); 
    HttpParams httpParams = new BasicHttpParams(); 
    HttpConnectionParams.setConnectionTimeout(httpParams,  connectionTimeoutMillis); 
    HttpConnectionParams.setSoTimeout(httpParams, socketTimeoutMillis); 
    HttpProtocolParams.setContentCharset(httpParams, "utf-8"); 

    httpClient = new DefaultHttpClient(httpParams); 
} 

public HttpRmi add(String key, String value){ 
    nameValuePairs.add(new BasicNameValuePair(key, value)); 
    return this; 
} 

public String execute() throws IOException{ 
    String response = ""; 
    try{ 
     request.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8") ); 

     response = httpClient.execute(request, new BasicResponseHandler()); 
    }catch(Exception e){ 
     e.printStackTrace(); 
     throw new IOException("no connection"); 
    } 

    return response; 
} 

}

+0

使用'HttpUrlConnection'作爲替代 – Enzokie

回答

0

Httpclient的最佳替代品使​​用OKHttpHttpUrlConnection

新的應用程序應該使用HttpURLConnection;這正是我們將會在 花費我們的能量前進的地方。 link

0

隨着HttpURLConnection的

 import java.net.HttpURLConnection; 
     import java.net.URL; 


     String retFromServer = ""; 
     URL url; 
     HttpURLConnection urlConnection = null; 
     try { 
      url = new URL("http://www.somedomain.com/); 
      urlConnection = (HttpURLConnection) url.openConnection(); 

      InputStream in = urlConnection.getInputStream(); 
      InputStreamReader inRead = new InputStreamReader(in); 

      retFromServer = IOUtils.toString(inRead); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      if (urlConnection != null) { 
       urlConnection.disconnect(); 
      } 
     } 
+0

不要我把它在構造函數中部分或在哪裏呢? – fatima

+0

在您想要建立http連接的方法中 –