本文整理汇总了Java中android.net.http.AndroidHttpClient类的典型用法代码示例。如果您正苦于以下问题:Java AndroidHttpClient类的具体用法?Java AndroidHttpClient怎么用?Java AndroidHttpClient使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AndroidHttpClient类属于android.net.http包,在下文中一共展示了AndroidHttpClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: newRequestQueue
import android.net.http.AndroidHttpClient; //导入依赖的package包/类
/**
* Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
*
* @param context A {@link Context} to use for creating the cache dir.
* @param stack An {@link HttpStack} to use for the network, or null for default.
* @return A started {@link RequestQueue} instance.
*/
public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);
String userAgent = "volley/0";
try {
String packageName = context.getPackageName();
PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
userAgent = packageName + "/" + info.versionCode;
} catch (NameNotFoundException e) {
}
if (stack == null) {
if (Build.VERSION.SDK_INT >= 9) {
stack = new HurlStack();
} else {
// Prior to Gingerbread, HttpUrlConnection was unreliable.
// See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
}
}
Network network = new BasicNetwork(stack);
RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
queue.start();
return queue;
}
示例2: downloadUriAsString
import android.net.http.AndroidHttpClient; //导入依赖的package包/类
private static String downloadUriAsString(final HttpUriRequest req)
throws IOException {
AndroidHttpClient client = AndroidHttpClient.newInstance(userAgentString());
try {
HttpResponse res = client.execute(req);
return readToEnd(res.getEntity().getContent());
} finally {
client.close();
}
}
示例3: createHttpClient
import android.net.http.AndroidHttpClient; //导入依赖的package包/类
private static AndroidHttpClient createHttpClient(Context context) {
String userAgent = MmsConfig.getUserAgent();
AndroidHttpClient client = AndroidHttpClient.newInstance(userAgent, context);
HttpParams params = client.getParams();
HttpProtocolParams.setContentCharset(params, "UTF-8");
// set the socket timeout
int soTimeout = MmsConfig.getHttpSocketTimeout();
if (DEBUG) {
Log.d(TAG, "[HttpUtils] createHttpClient w/ socket timeout " + soTimeout + " ms, "
+ ", UA=" + userAgent);
}
HttpConnectionParams.setSoTimeout(params, soTimeout);
return client;
}
示例4: executeDownload
import android.net.http.AndroidHttpClient; //导入依赖的package包/类
/**
* Fully execute a single download request - setup and send the request,
* handle the response, and transfer the data to the destination file.
*/
private void executeDownload(State state, AndroidHttpClient client,
HttpGet request) throws StopRequest, RetryDownload {
InnerState innerState = new InnerState();
byte data[] = new byte[Constants.BUFFER_SIZE];
setupDestinationFile(state, innerState);
addRequestHeaders(innerState, request);
// check just before sending the request to avoid using an invalid
// connection at all
checkConnectivity(state);
HttpResponse response = sendRequest(state, client, request);
handleExceptionalStatus(state, innerState, response);
if (Constants.LOGV) {
Log.v(Constants.TAG, "received response for " + mInfo.mUri);
}
processResponseHeaders(state, innerState, response);
InputStream entityStream = openResponseEntity(state, response);
transferData(state, innerState, data, entityStream);
}
示例5: getDefaultStack
import android.net.http.AndroidHttpClient; //导入依赖的package包/类
private static HttpStack getDefaultStack(Context context){
String userAgent = "volley/0";
try {
String packageName = context.getPackageName();
PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
userAgent = packageName + "/" + info.versionCode;
} catch (NameNotFoundException e) {
}
if (Build.VERSION.SDK_INT >= 9) {
return new HurlStack();
} else {
// Prior to Gingerbread, HttpUrlConnection was unreliable.
// See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
return new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
}
}
示例6: doInBackground
import android.net.http.AndroidHttpClient; //导入依赖的package包/类
@Override
protected String doInBackground(String... url) {
AndroidHttpClient httpClient = AndroidHttpClient.newInstance("test");
HttpGet get = new HttpGet("http://backend.applab.fhws.de:8080/fhws/simple");
try
{
HttpResponse response = httpClient.execute(get);
InputStream is = response.getEntity().getContent();
return IOUtils.toString(is);
}
catch( Exception e )
{
e.printStackTrace();
}
return "Error";
}
示例7: doInBackground
import android.net.http.AndroidHttpClient; //导入依赖的package包/类
@Override
protected String doInBackground( Void... params )
{
AndroidHttpClient httpClient = AndroidHttpClient.newInstance( "" );
HttpGet get = new HttpGet( URL );
try
{
HttpResponse response = httpClient.execute( get );
HttpEntity entity = response.getEntity( );
InputStreamReader reader = new InputStreamReader( entity.getContent( ) );
BufferedReader bufReader = new BufferedReader( reader );
String status = bufReader.readLine( );
entity.consumeContent( );
return status;
}
catch ( Exception e )
{
Log.e( "WIDGET", e.getMessage( ) );
}
return "No status received";
}
示例8: doInBackground
import android.net.http.AndroidHttpClient; //导入依赖的package包/类
@Override
protected VastVideoConfiguration doInBackground(@Nullable String... strings) {
AndroidHttpClient httpClient = null;
try {
httpClient = HttpClient.getHttpClient();
if (strings != null && strings.length > 0) {
String vastXml = strings[0];
if (vastXml == null) {
return null;
}
return evaluateVastXmlManager(vastXml, httpClient, new ArrayList<VastTracker>());
}
} catch (Exception e) {
MoPubLog.d("Failed to parse VAST XML", e);
} finally {
if (httpClient != null) {
httpClient.close();
}
}
return null;
}
示例9: evaluateWrapperRedirect
import android.net.http.AndroidHttpClient; //导入依赖的package包/类
/**
* Retrieves the Wrapper's redirect uri and follows it to return the next VAST xml String.
*
* @param vastWrapperXmlManager used to get the redirect uri
* @param androidHttpClient the http client
* @param wrapperErrorTrackers Error trackers to hit if something goes wrong
* @return the next VAST xml String or {@code null} if it could not be resolved
*/
@Nullable
private String evaluateWrapperRedirect(@NonNull VastWrapperXmlManager vastWrapperXmlManager,
@NonNull AndroidHttpClient androidHttpClient,
@NonNull List<VastTracker> wrapperErrorTrackers) {
String vastAdTagUri = vastWrapperXmlManager.getVastAdTagURI();
if (vastAdTagUri == null) {
return null;
}
String vastRedirectXml = null;
try {
vastRedirectXml = followVastRedirect(androidHttpClient, vastAdTagUri);
} catch (Exception e) {
MoPubLog.d("Failed to follow VAST redirect", e);
if (!wrapperErrorTrackers.isEmpty()) {
makeVastTrackingHttpRequest(wrapperErrorTrackers, VastErrorCode.WRAPPER_TIMEOUT,
null, null, mContext);
}
}
return vastRedirectXml;
}
示例10: followVastRedirect
import android.net.http.AndroidHttpClient; //导入依赖的package包/类
@Nullable
private String followVastRedirect(@NonNull final AndroidHttpClient httpClient,
@NonNull final String redirectUrl) throws Exception {
Preconditions.checkNotNull(httpClient);
Preconditions.checkNotNull(redirectUrl);
if (mTimesFollowedVastRedirect < MAX_TIMES_TO_FOLLOW_VAST_REDIRECT) {
mTimesFollowedVastRedirect++;
final HttpGet httpget = HttpClient.initializeHttpGet(redirectUrl);
final HttpResponse response = httpClient.execute(httpget);
final HttpEntity entity = response.getEntity();
return (entity != null) ? Strings.fromStream(entity.getContent()) : null;
}
return null;
}
示例11: doInBackground
import android.net.http.AndroidHttpClient; //导入依赖的package包/类
@Override
protected DownloadResponse doInBackground(final HttpUriRequest... httpUriRequests) {
if (httpUriRequests == null || httpUriRequests.length == 0 || httpUriRequests[0] == null) {
MoPubLog.d("Download task tried to execute null or empty url");
return null;
}
final HttpUriRequest httpUriRequest = httpUriRequests[0];
mUrl = httpUriRequest.getURI().toString();
AndroidHttpClient httpClient = null;
try {
httpClient = HttpClient.getHttpClient();
final HttpResponse httpResponse = httpClient.execute(httpUriRequest);
return new DownloadResponse(httpResponse);
} catch (Exception e) {
MoPubLog.d("Download task threw an internal exception", e);
return null;
} finally {
if (httpClient != null) {
httpClient.close();
}
}
}
示例12: SimpleHttpGet
import android.net.http.AndroidHttpClient; //导入依赖的package包/类
public static SimpleWebResponse SimpleHttpGet(AndroidHttpClient client, String url, String contentType, String callerDebugName) throws IOException{
String netResult = "", line;
HttpGet getRequest = new HttpGet(url);
getRequest.addHeader("accept", contentType);
HttpResponse response = client.execute(getRequest);
if (response.getStatusLine().getStatusCode() != 200) {
HttpResponseLog(response, callerDebugName);
return new SimpleWebResponse(null, false);
}else{
BufferedReader br = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));
while ((line = br.readLine()) != null) {
netResult = netResult + line;
}
}
return new SimpleWebResponse(netResult, true);
}
示例13: SimpleHttpPost
import android.net.http.AndroidHttpClient; //导入依赖的package包/类
public static SimpleWebResponse SimpleHttpPost(AndroidHttpClient client, String url, String body, String contentType, String callerDebugName) throws IOException {
String netResult = "", line;
HttpPost postRequest = new HttpPost(url);
postRequest.addHeader("accept", contentType);
postRequest.setEntity(new StringEntity(body, "UTF8"));
HttpResponse response = client.execute(postRequest);
if (response.getStatusLine().getStatusCode() != 200) {
HttpResponseLog(response, callerDebugName);
return new SimpleWebResponse(null, false);
}else{
BufferedReader br = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));
while ((line = br.readLine()) != null) {
netResult = netResult + line;
}
}
return new SimpleWebResponse(netResult, true);
}
示例14: AddServersFromOnlineSource
import android.net.http.AndroidHttpClient; //导入依赖的package包/类
private void AddServersFromOnlineSource(){
//Get updated servers list from BlochsTech.com/StratumServers.json
try{
AndroidHttpClient client = AndroidHttpClient.newInstance("BOBC-0 Terminal/0.0/Android");
SimpleWebResponse resp = WebUtil.SimpleHttpGet(client, "http://blochstech.com/content/StratumServers.txt", "text/plain", "StratumServerManager_GetBlochsTechServerList");
if(resp.IsConnected && resp.Response != null){
JSONObject json = new JSONObject(resp.Response);
JSONArray jsonServers = json.getJSONArray("Servers");
String url;
for(int i = 0; i < jsonServers.length(); i++){
url = jsonServers.getString(i);
if(!ContainsServer(url))
servers.add(new StratumServer(url));
}
}
}catch(Exception ex){
if(Tags.DEBUG)
Log.e(Tags.APP_TAG, "Failed to get StratumServers from online source. Ex: " + ex.toString());
}
}
示例15: newRequestQueue
import android.net.http.AndroidHttpClient; //导入依赖的package包/类
private static RequestQueue newRequestQueue(Context context) {
// On HC+ use HurlStack which is based on HttpURLConnection. Otherwise fall back on
// AndroidHttpClient (based on Apache DefaultHttpClient) which should no longer be used
// on newer platform versions where HttpURLConnection is simply better.
Network network = new BasicNetwork(
UIUtils.hasHoneycomb() ?
new HurlStack() :
new HttpClientStack(AndroidHttpClient.newInstance(
NetUtils.getUserAgent(context))));
Cache cache = new DiskBasedCache(getDiskCacheDir(context, CACHE_DIR));
RequestQueue queue = new RequestQueue(cache, network);
queue.start();
return queue;
}