本文整理汇总了Java中org.apache.http.impl.client.AbstractHttpClient.execute方法的典型用法代码示例。如果您正苦于以下问题:Java AbstractHttpClient.execute方法的具体用法?Java AbstractHttpClient.execute怎么用?Java AbstractHttpClient.execute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.impl.client.AbstractHttpClient
的用法示例。
在下文中一共展示了AbstractHttpClient.execute方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: execute
import org.apache.http.impl.client.AbstractHttpClient; //导入方法依赖的package包/类
/** 执行网络访问 */
private static HttpResult execute(String url, HttpRequestBase requestBase) {
boolean isHttps = url.startsWith("https://");//判断是否需要采用https
AbstractHttpClient httpClient = HttpClientFactory.create(isHttps);
HttpContext httpContext = new SyncBasicHttpContext(new BasicHttpContext());
HttpRequestRetryHandler retryHandler = httpClient.getHttpRequestRetryHandler();//获取重试机制
int retryCount = 0;
boolean retry = true;
while (retry) {
try {
HttpResponse response = httpClient.execute(requestBase, httpContext);//访问网络
if (response != null) {
return new HttpResult(response, httpClient, requestBase);
}
} catch (Exception e) {
IOException ioException = new IOException(e.getMessage());
retry = retryHandler.retryRequest(ioException, ++retryCount, httpContext);//把错误异常交给重试机制,以判断是否需要采取从事
}
}
return null;
}
示例2: execute
import org.apache.http.impl.client.AbstractHttpClient; //导入方法依赖的package包/类
/** 执行网络访问 */
private static HttpResult execute(String url, HttpRequestBase requestBase) {
boolean isHttps = url.startsWith("https://");//判断是否需要采用https
AbstractHttpClient httpClient = HttpClientFactory.create(isHttps);
HttpContext httpContext = new SyncBasicHttpContext(new BasicHttpContext());
HttpRequestRetryHandler retryHandler = httpClient.getHttpRequestRetryHandler();//获取重试机制
int retryCount = 0;
boolean retry = true;
while (retry) {
try {
HttpResponse response = httpClient.execute(requestBase, httpContext);//访问网络
if (response != null) {
return new HttpResult(response, httpClient, requestBase);
}
} catch (Exception e) {
IOException ioException = new IOException(e.getMessage());
retry = retryHandler.retryRequest(ioException, ++retryCount, httpContext);//把错误异常交给重试机制,以判断是否需要采取从事
LogUtils.e(e);
}
}
return null;
}
示例3: doRequest
import org.apache.http.impl.client.AbstractHttpClient; //导入方法依赖的package包/类
private static SimplHttpResponse doRequest(String userAgent,
final String url,
HttpUriRequest request) throws IOException
{
AbstractHttpClient client = factory.create(userAgent);
// client must use BasicRedirectStrategy!
final BasicRedirectStrategy redirectStrategy =
(BasicRedirectStrategy) client.getRedirectStrategy();
SimplHttpResponse result = client.execute(request, new ResponseHandler<SimplHttpResponse>()
{
@Override
public SimplHttpResponse handleResponse(HttpResponse response)
throws ClientProtocolException, IOException
{
List<String> redirectedLocations = redirectStrategy.getRedirectedLocations();
return SimplHttpResponse.parse(url, redirectedLocations, response);
}
});
return result;
}
示例4: execute
import org.apache.http.impl.client.AbstractHttpClient; //导入方法依赖的package包/类
/**
* 执行网络访问
*/
private static void execute(String url, HttpRequestBase requestBase, HttpCallbackListener httpCallbackListener) {
boolean isHttps = url.startsWith("https://");//判断是否需要采用https
AbstractHttpClient httpClient = HttpClientFactory.create(isHttps);
HttpContext httpContext = new SyncBasicHttpContext(new BasicHttpContext());
HttpRequestRetryHandler retryHandler = httpClient.getHttpRequestRetryHandler();//获取重试机制
int retryCount = 0;
boolean retry = true;
while (retry) {
try {
HttpResponse response = httpClient.execute(requestBase, httpContext);//访问网络
int stateCode = response.getStatusLine().getStatusCode();
// LogUtils.e(TAG, "http状态码:" + stateCode);
if (response != null) {
if (stateCode == HttpURLConnection.HTTP_OK){
HttpResult httpResult = new HttpResult(response, httpClient, requestBase);
String result = httpResult.getString();
if (!TextUtils.isEmpty(result)){
httpCallbackListener.onSuccess(result);
return;
} else {
throw new RuntimeException("数据为空");
}
} else {
throw new RuntimeException(HttpRequestCode.ReturnCode(stateCode));
}
}
} catch (Exception e) {
IOException ioException = new IOException(e.getMessage());
retry = retryHandler.retryRequest(ioException, ++retryCount, httpContext);//把错误异常交给重试机制,以判断是否需要采取从事
LogUtils.e(TAG, "重复次数:" + retryCount + " :"+ e);
if (!retry){
httpCallbackListener.onError(e);
}
}
}
}
示例5: myClickHandler
import org.apache.http.impl.client.AbstractHttpClient; //导入方法依赖的package包/类
public void myClickHandler(View view) {
switch (view.getId()) {
case R.id.ReadWebPage:
try {
textView.setText("");
// Cast to AbstractHttpClient to have access to
// setHttpRequestRetryHandler
AbstractHttpClient client = (AbstractHttpClient) new DefaultHttpClient();
HttpGet request = new HttpGet(urlText.getText().toString());
HttpResponse response = client.execute(request);
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
textView.append(line);
}
}
catch (Exception e) {
System.out.println("Nay, did not work");
textView.setText(e.getMessage());
}
break;
}
}
示例6: getHttpDownload
import org.apache.http.impl.client.AbstractHttpClient; //导入方法依赖的package包/类
/**
* 读取文件内容
*/
public static String getHttpDownload(String url, String baseFolder) {
// TODO Auto-generated method stub
byte buffer[] = null;
int byteread = 0;
int bytesum = 0;
StringBuffer sb = new StringBuffer();
try {
AbstractHttpClient httpclient = new DefaultHttpClient();// 创建一个客户端,类似打开一个浏览器
httpclient.getParams().setParameter("http.connection.timeout", 100000);
HttpGet get = new HttpGet(url);// 创建一个get方法,类似在浏览器地址栏中输入一个地址
get.setHeader("Accept", "Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
get.setHeader("Accept-Language", "zh-cn,zh;q=0.5");
get.setHeader("Connection", "keep-alive");
get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36");
long t = System.currentTimeMillis();
HttpResponse httpResponse = httpclient.execute(get);
System.out.println(url + "请求结束:" + (System.currentTimeMillis() - t));
int statusCode = httpResponse.getStatusLine()
.getStatusCode();
if (HttpStatus.SC_OK == statusCode) {
HttpEntity ent = httpResponse.getEntity();
buffer = new byte[1024];
InputStream in = ent.getContent();
// fo=new FileOutputStream(new File(baseFolder+File.separator+filename),true);
while ((byteread = in.read(buffer)) != -1) {
bytesum += byteread;
sb.append(new String(buffer, 0, byteread, "UTF-8"));
// fo.write(buffer, 0, byteread);
}
// fo.close();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return sb.toString();
}
示例7: getHttpDownloadFile
import org.apache.http.impl.client.AbstractHttpClient; //导入方法依赖的package包/类
/**
* 下载文件
*/
public static String getHttpDownloadFile(String url, String baseFolder) {
// TODO Auto-generated method stub
byte buffer[] = null;
int byteread = 0;
int bytesum = 0;
FileOutputStream fo = null;
StringBuffer sb = new StringBuffer();
String fileName = System.currentTimeMillis() + ".json";
try {
AbstractHttpClient httpclient = new DefaultHttpClient();// 创建一个客户端,类似打开一个浏览器
httpclient.getParams().setParameter("http.connection.timeout", 100000);
HttpGet get = new HttpGet(url);// 创建一个get方法,类似在浏览器地址栏中输入一个地址
get.setHeader("Accept", "Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
get.setHeader("Accept-Language", "zh-cn,zh;q=0.5");
get.setHeader("Connection", "keep-alive");
get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36");
long t = System.currentTimeMillis();
HttpResponse httpResponse = httpclient.execute(get);
System.out.println(url + "请求结束:" + (System.currentTimeMillis() - t));
int statusCode = httpResponse.getStatusLine()
.getStatusCode();
if (HttpStatus.SC_OK == statusCode) {
HttpEntity ent = httpResponse.getEntity();
buffer = new byte[1024];
InputStream in = ent.getContent();
fo = new FileOutputStream(new File(baseFolder + File.separator + fileName), true);
while ((byteread = in.read(buffer)) != -1) {
bytesum += byteread;
// sb.append(new String(buffer, 0, byteread,"UTF-8"));
fo.write(buffer, 0, byteread);
}
fo.close();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return sb.toString();
}
示例8: retrieveLatestVersion
import org.apache.http.impl.client.AbstractHttpClient; //导入方法依赖的package包/类
/**
* Retrieves the latest version number of the app or search module by checking an online text file that looks like
* '160|1.1.15' for version code 160 and version name 1.1.15.
* @param httpclient An already instantiated HTTP client
* @param url The URL of the the text file that contains the current latest version code and name
* @return A string array with two elements: the version code and the version number
* @throws ClientProtocolException Thrown when the provided URL is invalid
* @throws IOException Thrown when the last version information could not be retrieved
*/
private String[] retrieveLatestVersion(AbstractHttpClient httpclient, String url) throws IOException {
HttpResponse request = httpclient.execute(new HttpGet(url));
InputStream stream = request.getEntity().getContent();
String appVersion[] = HttpHelper.convertStreamToString(stream).split("\\|");
stream.close();
return appVersion;
}