本文整理汇总了Java中cz.msebera.android.httpclient.impl.client.DefaultHttpClient类的典型用法代码示例。如果您正苦于以下问题:Java DefaultHttpClient类的具体用法?Java DefaultHttpClient怎么用?Java DefaultHttpClient使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DefaultHttpClient类属于cz.msebera.android.httpclient.impl.client包,在下文中一共展示了DefaultHttpClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getNewHttpClient
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient; //导入依赖的package包/类
/**
* Gets a DefaultHttpClient which trusts a set of certificates specified by the KeyStore
*
* @param keyStore custom provided KeyStore instance
* @return DefaultHttpClient
*/
public static DefaultHttpClient getNewHttpClient(KeyStore keyStore) {
try {
SSLSocketFactory sf = new MySSLSocketFactory(keyStore);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", sf, 443));
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
return new DefaultHttpClient(ccm, params);
} catch (Exception e) {
return new DefaultHttpClient();
}
}
示例2: sendRequest
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient; //导入依赖的package包/类
@Override
protected RequestHandle sendRequest(DefaultHttpClient client,
HttpContext httpContext, HttpUriRequest uriRequest,
String contentType, ResponseHandlerInterface responseHandler,
Context context) {
if (contentType != null) {
uriRequest.addHeader(AsyncHttpClient.HEADER_CONTENT_TYPE, contentType);
}
responseHandler.setUseSynchronousMode(true);
/*
* will execute the request directly
*/
newAsyncHttpRequest(client, httpContext, uriRequest, contentType, responseHandler, context).run();
// Return a Request Handle that cannot be used to cancel the request
// because it is already complete by the time this returns
return new RequestHandle(null);
}
示例3: get
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient; //导入依赖的package包/类
public static Hostplace get(String ip) throws Exception {
HttpGet httppost = new HttpGet(Geocode.url + "/" + ip);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject json = new JSONObject(data);
if (json.getString("status").equals("fail"))
throw new Exception();
Hostplace hostplace = Hostplace.getJson(json);
return hostplace;
}
throw new Exception();
}
示例4: getBlock
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient; //导入依赖的package包/类
public static Block getBlock(String blockhash) throws Exception {
HttpGet httppost = new HttpGet(Insight.url + "/block/" + blockhash);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject json = new JSONObject(data);
Block block = Block.getJson(json);
return block;
}
throw new Exception();
}
示例5: getBlockHash
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient; //导入依赖的package包/类
public static String getBlockHash(String height) throws Exception {
HttpGet httppost = new HttpGet(Insight.url + "/block-index/" + height);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject json = new JSONObject(data);
String hash = json.getString("blockHash");
return hash;
}
throw new Exception();
}
示例6: getTx
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient; //导入依赖的package包/类
public static Tx getTx(String hash) throws Exception {
HttpGet httppost = new HttpGet(Insight.url + "/tx/" + hash);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject json = new JSONObject(data);
Tx tx = Tx.getJson(json);
return tx;
}
throw new Exception();
}
示例7: createMyHttpClient
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient; //导入依赖的package包/类
public static HttpClient createMyHttpClient() {
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory mSSLSocketFactory = new IgnoreSSLSocketFactory(trustStore);
mSSLSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", mSSLSocketFactory, 443));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
return new DefaultHttpClient(ccm, params);
} catch (KeyStoreException | NoSuchAlgorithmException | IOException | CertificateException | KeyManagementException | UnrecoverableKeyException e) {
e.printStackTrace();
}
return new DefaultHttpClient();
}
示例8: newAsyncHttpRequest
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient; //导入依赖的package包/类
@Override
protected AsyncHttpRequest newAsyncHttpRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler, Context context) {
AsyncHttpRequest httpRequest = getHttpRequest(client, httpContext, uriRequest, contentType, responseHandler, context);
return httpRequest == null
? super.newAsyncHttpRequest(client, httpContext, uriRequest, contentType, responseHandler, context)
: httpRequest;
}
示例9: getAsyncHttpClient
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient; //导入依赖的package包/类
@Override
public AsyncHttpClient getAsyncHttpClient() {
AsyncHttpClient ahc = super.getAsyncHttpClient();
HttpClient client = ahc.getHttpClient();
if (client instanceof DefaultHttpClient) {
Toast.makeText(this,
String.format("redirects: %b\nrelative redirects: %b\ncircular redirects: %b",
enableRedirects, enableRelativeRedirects, enableCircularRedirects),
Toast.LENGTH_SHORT
).show();
ahc.setEnableRedirects(enableRedirects, enableRelativeRedirects, enableCircularRedirects);
}
return ahc;
}
示例10: doInBackground
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient; //导入依赖的package包/类
@Override
protected Void doInBackground(String... message) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.instruman.it/assets/feedback/update.php");
try {
//add data
List<NameValuePair> nameValuePairs = new ArrayList<>(1);
nameValuePairs.add(new BasicNameValuePair("stars", message[0]));
nameValuePairs.add(new BasicNameValuePair("message", message[1]));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//execute http post
HttpResponse response = httpclient.execute(httppost);
} catch (Exception e) {
}
return null;
}
示例11: init
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient; //导入依赖的package包/类
public static void init() {
synchronized (HttpClientProxy.class) {
if (mHttpClient == null) {
mHttpClient = new AsyncHttpClient();
mHttpClient.setTimeout(15 * 1000);
mHttpClient.setMaxRetriesAndTimeout(1, 1000);
HttpClient client = mHttpClient.getHttpClient();
if (client instanceof DefaultHttpClient) {
mHttpClient.setEnableRedirects(true, true, true);
}
}
}
}
示例12: getHttpRequest
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient; //导入依赖的package包/类
@Override
public AsyncHttpRequest getHttpRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler, Context context) {
return null;
}
示例13: getHttpRequest
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient; //导入依赖的package包/类
@Override
public AsyncHttpRequest getHttpRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler, Context context) {
return new PrePostProcessRequest(client, httpContext, uriRequest, responseHandler);
}
示例14: sendRequest
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient; //导入依赖的package包/类
/**
* Puts a new request in queue as a new thread in pool to be executed
*
* @param client HttpClient to be used for request, can differ in single requests
* @param contentType MIME body type, for POST and PUT requests, may be null
* @param context Context of Android application, to hold the reference of request
* @param httpContext HttpContext in which the request will be executed
* @param responseHandler ResponseHandler or its subclass to put the response into
* @param uriRequest instance of HttpUriRequest, which means it must be of HttpDelete,
* HttpPost, HttpGet, HttpPut, etc.
* @return RequestHandle of future request process
*/
protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler, Context context) {
if (uriRequest == null) {
throw new IllegalArgumentException("HttpUriRequest must not be null");
}
if (responseHandler == null) {
throw new IllegalArgumentException("ResponseHandler must not be null");
}
if (responseHandler.getUseSynchronousMode() && !responseHandler.getUsePoolThread()) {
throw new IllegalArgumentException("Synchronous ResponseHandler used in AsyncHttpClient. You should create your response handler in a looper thread or use SyncHttpClient instead.");
}
if (contentType != null) {
if (uriRequest instanceof HttpEntityEnclosingRequestBase && ((HttpEntityEnclosingRequestBase) uriRequest).getEntity() != null && uriRequest.containsHeader(HEADER_CONTENT_TYPE)) {
log.w(LOG_TAG, "Passed contentType will be ignored because HttpEntity sets content type");
} else {
uriRequest.setHeader(HEADER_CONTENT_TYPE, contentType);
}
}
responseHandler.setRequestHeaders(uriRequest.getAllHeaders());
responseHandler.setRequestURI(uriRequest.getURI());
AsyncHttpRequest request = newAsyncHttpRequest(client, httpContext, uriRequest, contentType, responseHandler, context);
threadPool.submit(request);
RequestHandle requestHandle = new RequestHandle(request);
if (context != null) {
List<RequestHandle> requestList;
// Add request to request map
synchronized (requestMap) {
requestList = requestMap.get(context);
if (requestList == null) {
requestList = Collections.synchronizedList(new LinkedList<RequestHandle>());
requestMap.put(context, requestList);
}
}
requestList.add(requestHandle);
Iterator<RequestHandle> iterator = requestList.iterator();
while (iterator.hasNext()) {
if (iterator.next().shouldBeGarbageCollected()) {
iterator.remove();
}
}
}
return requestHandle;
}
示例15: newAsyncHttpRequest
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient; //导入依赖的package包/类
@Override
protected AsyncHttpRequest newAsyncHttpRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler, Context context) {
return new CheckNetAsyncHttpRequest(client, httpContext, uriRequest, responseHandler);
}