本文整理匯總了Java中org.apache.http.client.methods.HttpGet.abort方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpGet.abort方法的具體用法?Java HttpGet.abort怎麽用?Java HttpGet.abort使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.http.client.methods.HttpGet
的用法示例。
在下文中一共展示了HttpGet.abort方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getText
import org.apache.http.client.methods.HttpGet; //導入方法依賴的package包/類
public static String getText(String redirectLocation) {
HttpGet httpget = new HttpGet(redirectLocation);
// Create a response handler
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = "";
try {
responseBody = httpclient.execute(httpget, responseHandler);
} catch (Exception e) {
e.printStackTrace();
responseBody = null;
} finally {
httpget.abort();
// httpclient.getConnectionManager().shutdown();
}
return responseBody;
}
示例2: doGet
import org.apache.http.client.methods.HttpGet; //導入方法依賴的package包/類
/**
* HTTP Get 獲取內容
*
* @param url 請求的url地址 ?之前的地址
* @param params 請求的參數
* @param charset 編碼格式
* @return 頁麵內容
*/
public static String doGet(String url, Map<String, String> params, String charset) throws Exception {
if (StringUtils.isBlank(url)) {
return null;
}
try {
if (params != null && !params.isEmpty()) {
List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());
for (Map.Entry<String, String> entry : params.entrySet()) {
String value = entry.getValue();
if (value != null) {
pairs.add(new BasicNameValuePair(entry.getKey(), value));
}
}
url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, charset));
}
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = httpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200) {
httpGet.abort();
throw new RuntimeException("HttpClient,error status code :" + statusCode);
}
HttpEntity entity = response.getEntity();
String result = null;
if (entity != null) {
result = EntityUtils.toString(entity, "utf-8");
}
EntityUtils.consume(entity);
response.close();
return result;
} catch (Exception e) {
throw e;
}
}
示例3: doHttpGet
import org.apache.http.client.methods.HttpGet; //導入方法依賴的package包/類
public String doHttpGet(Context context, StatisCacheBean mStatisCacheBean) throws HttpDataConnectionException, HttpDataParserException {
String str = null;
if (context != null) {
if (DataStatistics.getInstance().isDebug()) {
Log.d(DataStatistics.TAG, "url:" + mStatisCacheBean.getCacheData());
}
if (mStatisCacheBean != null) {
StatisDBHandler.saveLocalCache(context, mStatisCacheBean);
if (DataUtils.getAvailableNetWorkInfo(context) != null) {
HttpGet httpGet = new HttpGet(mStatisCacheBean.getCacheData());
try {
HttpResponse httpResponse = this.mDefaultHttpClient.execute(httpGet);
if (httpResponse == null) {
try {
httpGet.abort();
this.mDefaultHttpClient.getConnectionManager().closeExpiredConnections();
} catch (Exception e) {
e.printStackTrace();
}
} else {
int responseCode = httpResponse.getStatusLine().getStatusCode();
if (DataStatistics.getInstance().isDebug()) {
Log.d(DataStatistics.TAG, "responseCode:" + responseCode);
}
if (responseCode < 200 || responseCode >= 300) {
if (!DataStatistics.getInstance().isDebug() && (System.currentTimeMillis() - mStatisCacheBean.getCacheTime()) / 1000 >= 432000) {
StatisDBHandler.deleteByCacheId(context, mStatisCacheBean.getCacheId());
}
throw new HttpDataConnectionException(httpResponse.getStatusLine().toString());
} else if (httpResponse.getEntity() == null) {
try {
httpGet.abort();
this.mDefaultHttpClient.getConnectionManager().closeExpiredConnections();
} catch (Exception e2) {
e2.printStackTrace();
}
} else {
str = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
if (DataStatistics.getInstance().isDebug()) {
Log.d(DataStatistics.TAG, "result:" + str);
}
StatisDBHandler.deleteByCacheId(context, mStatisCacheBean.getCacheId());
try {
httpGet.abort();
this.mDefaultHttpClient.getConnectionManager().closeExpiredConnections();
} catch (Exception e22) {
e22.printStackTrace();
}
}
}
} catch (Exception e222) {
e222.printStackTrace();
throw new HttpDataParserException(e222);
} catch (Exception e2222) {
e2222.printStackTrace();
throw new HttpDataConnectionException(e2222);
} catch (Exception e22222) {
e22222.printStackTrace();
throw new HttpDataConnectionException(e22222);
} catch (Exception e222222) {
e222222.printStackTrace();
throw new HttpDataConnectionException(e222222);
} catch (Throwable th) {
try {
httpGet.abort();
this.mDefaultHttpClient.getConnectionManager().closeExpiredConnections();
} catch (Exception e2222222) {
e2222222.printStackTrace();
}
}
}
}
}
return str;
}
示例4: run
import org.apache.http.client.methods.HttpGet; //導入方法依賴的package包/類
/**
* Executes the download in a separate thread
*/
public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
State state = new State(mInfo, mService);
AndroidHttpClient client = null;
PowerManager.WakeLock wakeLock = null;
int finalStatus = DownloaderService.STATUS_UNKNOWN_ERROR;
try {
PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, Constants.TAG);
wakeLock.acquire();
if (Constants.LOGV) {
Log.v(Constants.TAG, "initiating download for " + mInfo.mFileName);
Log.v(Constants.TAG, " at " + mInfo.mUri);
}
client = AndroidHttpClient.newInstance(userAgent(), mContext);
boolean finished = false;
while (!finished) {
if (Constants.LOGV) {
Log.v(Constants.TAG, "initiating download for " + mInfo.mFileName);
Log.v(Constants.TAG, " at " + mInfo.mUri);
}
// Set or unset proxy, which may have changed since last GET
// request.
// setDefaultProxy() supports null as proxy parameter.
ConnRouteParams.setDefaultProxy(client.getParams(),
getPreferredHttpHost(mContext, state.mRequestUri));
HttpGet request = new HttpGet(state.mRequestUri);
try {
executeDownload(state, client, request);
finished = true;
} catch (RetryDownload exc) {
// fall through
} finally {
request.abort();
request = null;
}
}
if (Constants.LOGV) {
Log.v(Constants.TAG, "download completed for " + mInfo.mFileName);
Log.v(Constants.TAG, " at " + mInfo.mUri);
}
finalizeDestinationFile(state);
finalStatus = DownloaderService.STATUS_SUCCESS;
} catch (StopRequest error) {
// remove the cause before printing, in case it contains PII
Log.w(Constants.TAG,
"Aborting request for download " + mInfo.mFileName + ": " + error.getMessage());
error.printStackTrace();
finalStatus = error.mFinalStatus;
// fall through to finally block
} catch (Throwable ex) { // sometimes the socket code throws unchecked
// exceptions
Log.w(Constants.TAG, "Exception for " + mInfo.mFileName + ": " + ex);
finalStatus = DownloaderService.STATUS_UNKNOWN_ERROR;
// falls through to the code that reports an error
} finally {
if (wakeLock != null) {
wakeLock.release();
wakeLock = null;
}
if (client != null) {
client.close();
client = null;
}
cleanupDestination(state, finalStatus);
notifyDownloadCompleted(finalStatus, state.mCountRetry, state.mRetryAfter,
state.mRedirectCount, state.mGotData, state.mFilename);
}
}