Java Code Examples for org.apache.http.impl.client.AbstractHttpClient#execute()
The following examples show how to use
org.apache.http.impl.client.AbstractHttpClient#execute() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpHelper.java From miappstore with Apache License 2.0 | 6 votes |
/** 执行网络访问 */ 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; }
Example 2
Source File: HttpHelper.java From NetEasyNews with GNU General Public License v3.0 | 5 votes |
/** * 执行网络访问 */ 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); } } } }
Example 3
Source File: ReadWebpage.java From codeexamples-android with Eclipse Public License 1.0 | 5 votes |
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; } }
Example 4
Source File: HttpHelper.java From elasticsearch-rest-command with The Unlicense | 4 votes |
/** * 读取文件内容 */ 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(); }
Example 5
Source File: HttpHelper.java From elasticsearch-rest-command with The Unlicense | 4 votes |
/** * 下载文件 */ 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(); }