Java源码示例:cz.msebera.android.httpclient.impl.client.DefaultHttpClient
示例1
public static String getContent(String url) throws Exception {
StringBuilder sb = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpParams httpParams = (HttpParams) client.getParams();
// 设置网络超时参数
HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
HttpConnectionParams.setSoTimeout(httpParams, 5000);
HttpResponse response = client.execute(new HttpGet(url));
HttpEntity entity = response.getEntity();
if (entity != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(
entity.getContent(), "GBK"), 8192);
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
}
return sb.toString();
}
示例2
@Override
protected String doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity httpEntity = response.getEntity();
return EntityUtils.toString(httpEntity);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
示例3
@Override
protected String doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity httpEntity = response.getEntity();
return EntityUtils.toString(httpEntity);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
示例4
@Override
protected String doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity httpEntity = response.getEntity();
return EntityUtils.toString(httpEntity);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
示例5
@Override
protected String doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity httpEntity = response.getEntity();
return EntityUtils.toString(httpEntity);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
示例6
@Override
protected String doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(final_url);
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity httpEntity = response.getEntity();
return EntityUtils.toString(httpEntity);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
示例7
@Override
protected String doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(getResources().getString(R.string.get_user_info_url) + token);
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity httpEntity = response.getEntity();
return EntityUtils.toString(httpEntity);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
示例8
@Override
protected String doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
try {
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpClient.execute(httpGet);
HttpEntity httpEntity = response.getEntity();
return EntityUtils.toString(httpEntity);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
示例9
public static String getResponseFromGetUrl(String url,
String params) throws Exception {
Log.d("Chen", "url--" + url);
if (null != params && !"".equals(params)) {
url = url + "?";
String[] paramarray = params.split(",");
for (int index = 0; null != paramarray && index < paramarray.length; index++) {
if (index == 0) {
url = url + paramarray[index];
} else {
url = url + "&" + paramarray[index];
}
}
}
HttpGet httpRequest = new HttpGet(url);
// httpRequest.addHeader("Cookie", logininfo);
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
int timeoutConnection = 30000;
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 30000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);
// DefaultHttpClient httpclient = new DefaultHttpClient();
StringBuffer sb = new StringBuffer();
try {
HttpResponse httpResponse = httpclient.execute(httpRequest);
String inputLine = "";
// Log.d("Chen","httpResponse.getStatusLine().getStatusCode()"+httpResponse.getStatusLine().getStatusCode());
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
InputStreamReader is = new InputStreamReader(httpResponse
.getEntity().getContent());
BufferedReader in = new BufferedReader(is);
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
}
in.close();
} else if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
return "";
}
} catch (Exception e) {
e.printStackTrace();
return "";
} finally {
httpclient.getConnectionManager().shutdown();
}
return sb.toString();
}