本文整理汇总了Java中com.loopj.android.http.AsyncHttpClient类的典型用法代码示例。如果您正苦于以下问题:Java AsyncHttpClient类的具体用法?Java AsyncHttpClient怎么用?Java AsyncHttpClient使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
AsyncHttpClient类属于com.loopj.android.http包,在下文中一共展示了AsyncHttpClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: executeSample
import com.loopj.android.http.AsyncHttpClient; //导入依赖的package包/类
@Override
public RequestHandle executeSample(final AsyncHttpClient client, final String URL, final Header[] headers, HttpEntity entity, final ResponseHandlerInterface responseHandler) {
if (client instanceof SyncHttpClient) {
new Thread(new Runnable() {
@Override
public void run() {
Log.d(LOG_TAG, "Before Request");
client.get(SynchronousClientSample.this, URL, headers, null, responseHandler);
Log.d(LOG_TAG, "After Request");
}
}).start();
} else {
Log.e(LOG_TAG, "Error, not using SyncHttpClient");
}
/**
* SyncHttpClient does not return RequestHandle,
* it executes each request directly,
* therefore those requests are not in cancelable threads
* */
return null;
}
示例2: executeSample
import com.loopj.android.http.AsyncHttpClient; //导入依赖的package包/类
@Override
public RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) {
RequestParams params = new RequestParams();
params.setUseJsonStreamer(true);
JSONObject body;
if (isRequestBodyAllowed() && (body = getBodyTextAsJSON()) != null) {
try {
Iterator keys = body.keys();
Log.d(LOG_TAG, "JSON data:");
while (keys.hasNext()) {
String key = (String) keys.next();
Log.d(LOG_TAG, " " + key + ": " + body.get(key));
params.put(key, body.get(key).toString());
}
} catch (JSONException e) {
Log.w(LOG_TAG, "Unable to retrieve a JSON value", e);
}
}
return client.post(this, URL, headers, params,
RequestParams.APPLICATION_JSON, responseHandler);
}
示例3: executeSample
import com.loopj.android.http.AsyncHttpClient; //导入依赖的package包/类
@Override
public RequestHandle executeSample(final AsyncHttpClient client, final String URL, final Header[] headers, HttpEntity entity, final ResponseHandlerInterface responseHandler) {
final Activity ctx = this;
FutureTask<RequestHandle> future = new FutureTask<>(new Callable<RequestHandle>() {
public RequestHandle call() {
Log.d(LOG_TAG, "Executing GET request on background thread");
return client.get(ctx, URL, headers, null, responseHandler);
}
});
executor.execute(future);
RequestHandle handle = null;
try {
handle = future.get(5, TimeUnit.SECONDS);
Log.d(LOG_TAG, "Background thread for GET request has finished");
} catch (Exception e) {
Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
return handle;
}
示例4: onCreate
import com.loopj.android.http.AsyncHttpClient; //导入依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The following exceptions will be whitelisted, i.e.: When an exception
// of this type is raised, the request will be retried.
AsyncHttpClient.allowRetryExceptionClass(IOException.class);
AsyncHttpClient.allowRetryExceptionClass(SocketTimeoutException.class);
AsyncHttpClient.allowRetryExceptionClass(ConnectTimeoutException.class);
// The following exceptions will be blacklisted, i.e.: When an exception
// of this type is raised, the request will not be retried and it will
// fail immediately.
AsyncHttpClient.blockRetryExceptionClass(UnknownHostException.class);
AsyncHttpClient.blockRetryExceptionClass(ConnectionPoolTimeoutException.class);
}
示例5: executeSample
import com.loopj.android.http.AsyncHttpClient; //导入依赖的package包/类
@Override
public RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) {
RequestParams rParams = new RequestParams();
rParams.put("sample_key", "Sample String");
try {
File sample_file = File.createTempFile("temp_", "_handled", getCacheDir());
rParams.put("sample_file", sample_file);
} catch (IOException e) {
Log.e(LOG_TAG, "Cannot add sample file", e);
}
return client.post(this, URL, headers, rParams, "multipart/form-data", responseHandler);
}
示例6: executeSample
import com.loopj.android.http.AsyncHttpClient; //导入依赖的package包/类
@Override
public RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) {
try {
RequestParams params = new RequestParams();
final String contentType = RequestParams.APPLICATION_OCTET_STREAM;
params.put("fileOne", createTempFile("fileOne", 1020), contentType, "fileOne");
params.put("fileTwo", createTempFile("fileTwo", 1030), contentType);
params.put("fileThree", createTempFile("fileThree", 1040), contentType, "customFileThree");
params.put("fileFour", createTempFile("fileFour", 1050), contentType);
params.put("fileFive", createTempFile("fileFive", 1060), contentType, "testingFileFive");
params.setHttpEntityIsRepeatable(true);
params.setUseJsonStreamer(false);
return client.post(this, URL, params, responseHandler);
} catch (FileNotFoundException fnfException) {
Log.e(LOG_TAG, "executeSample failed with FileNotFoundException", fnfException);
}
return null;
}
示例7: b
import com.loopj.android.http.AsyncHttpClient; //导入依赖的package包/类
private static boolean b(HttpUriRequest httpUriRequest) {
Header[] headers = httpUriRequest.getHeaders("content-encoding");
if (headers != null) {
for (Header value : headers) {
if (AsyncHttpClient.ENCODING_GZIP.equalsIgnoreCase(value.getValue())) {
return true;
}
}
}
Header[] headers2 = httpUriRequest.getHeaders(d.d);
if (headers2 == null) {
return true;
}
for (Header header : headers2) {
for (String startsWith : b) {
if (header.getValue().startsWith(startsWith)) {
return false;
}
}
}
return true;
}
示例8: c
import com.loopj.android.http.AsyncHttpClient; //导入依赖的package包/类
private HttpUriRequest c() {
if (this.f != null) {
return this.f;
}
if (this.j == null) {
byte[] b = this.c.b();
CharSequence b2 = this.c.b(AsyncHttpClient.ENCODING_GZIP);
if (b != null) {
if (TextUtils.equals(b2, "true")) {
this.j = b.a(b);
} else {
this.j = new ByteArrayEntity(b);
}
this.j.setContentType(this.c.c());
}
}
HttpEntity httpEntity = this.j;
if (httpEntity != null) {
HttpUriRequest httpPost = new HttpPost(b());
httpPost.setEntity(httpEntity);
this.f = httpPost;
} else {
this.f = new HttpGet(b());
}
return this.f;
}
示例9: Register
import com.loopj.android.http.AsyncHttpClient; //导入依赖的package包/类
public void Register(View v){
HttpClientHelper helper=new HttpClientHelper(new AsyncHttpClient());
final EditText name=(EditText)findViewById(R.id.name);
final EditText email=(EditText)findViewById(R.id.email);
final EditText pw=(EditText)findViewById(R.id.pw);
final EditText pwc=(EditText)findViewById(R.id.pwc);
if(pw.getText().toString().equals(pwc.getText().toString())) {
helper.postRegister(name.getText().toString(), email.getText().toString(), pw.getText().toString(),this);
}
else {
Toast.makeText(this, "Passwords are different", Toast.LENGTH_SHORT).show();
}
}
示例10: setBingImg
import com.loopj.android.http.AsyncHttpClient; //导入依赖的package包/类
private void setBingImg() {
AsyncHttpClient client = new AsyncHttpClient();
client.get("https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1", new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
if (statusCode == 200) {
Gson gson = new Gson();
BingImage bingImage = gson.fromJson(response.toString(), BingImage.class);
List<BingImage.ImagesBean> img = bingImage.getImages();
if (img != null && img.size() > 0) {
backImgUrl = "https://cn.bing.com" + img.get(0).getUrl();
setBackgroundImage();
} else Log.d("main", "onSuccess: 没有获取到类型");
}
}
}
);
}
示例11: setHttpClient
import com.loopj.android.http.AsyncHttpClient; //导入依赖的package包/类
public static void setHttpClient(AsyncHttpClient c, Application application) {
c.addHeader("Accept-Language", Locale.getDefault().toString());
c.addHeader("Host", HOST);
c.addHeader("Connection", "Keep-Alive");
//noinspection deprecation
c.getHttpClient().getParams()
.setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
// Set AppToken
c.addHeader("AppToken", Verifier.getPrivateToken(application));
//c.addHeader("AppToken", "123456");
// setUserAgent
c.setUserAgent(ApiClientHelper.getUserAgent(AppContext.getInstance()));
CLIENT = c;
initSSL(CLIENT);
initSSL(API.mClient);
}
示例12: cleanCookie
import com.loopj.android.http.AsyncHttpClient; //导入依赖的package包/类
public static void cleanCookie() {
// first clear store
// new PersistentCookieStore(AppContext.getInstance()).clear();
// clear header
AsyncHttpClient client = CLIENT;
if (client != null) {
HttpContext httpContext = client.getHttpContext();
CookieStore cookies = (CookieStore) httpContext
.getAttribute(HttpClientContext.COOKIE_STORE);
// 清理Async本地存储
if (cookies != null) {
cookies.clear();
}
// 清理当前正在使用的Cookie
client.removeHeader("Cookie");
}
log("cleanCookie");
}
示例13: getClientCookie
import com.loopj.android.http.AsyncHttpClient; //导入依赖的package包/类
/**
* 从AsyncHttpClient自带缓存中获取CookieString
*
* @param client AsyncHttpClient
* @return CookieString
*/
private static String getClientCookie(AsyncHttpClient client) {
String cookie = "";
if (client != null) {
HttpContext httpContext = client.getHttpContext();
CookieStore cookies = (CookieStore) httpContext
.getAttribute(HttpClientContext.COOKIE_STORE);
if (cookies != null && cookies.getCookies() != null && cookies.getCookies().size() > 0) {
for (Cookie c : cookies.getCookies()) {
cookie += (c.getName() + "=" + c.getValue()) + ";";
}
}
}
log("getClientCookie:" + cookie);
return cookie;
}
示例14: initSSL
import com.loopj.android.http.AsyncHttpClient; //导入依赖的package包/类
private static void initSSL(AsyncHttpClient client) {
try {
/// We initialize a default Keystore
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
// We load the KeyStore
trustStore.load(null, null);
// We initialize a new SSLSocketFacrory
MySSLSocketFactory socketFactory = new MySSLSocketFactory(trustStore);
// We set that all host names are allowed in the socket factory
socketFactory.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
// We set the SSL Factory
client.setSSLSocketFactory(socketFactory);
// We initialize a GET http request
} catch (Exception e) {
e.printStackTrace();
}
}
示例15: networkRequest
import com.loopj.android.http.AsyncHttpClient; //导入依赖的package包/类
private Request networkRequest(Request request) throws IOException {
Request.Builder result = request.newBuilder();
if (request.header("Host") == null) {
result.header("Host", Util.hostHeader(request.httpUrl()));
}
if (request.header("Connection") == null) {
result.header("Connection", "Keep-Alive");
}
if (request.header(AsyncHttpClient.HEADER_ACCEPT_ENCODING) == null) {
this.transparentGzip = true;
result.header(AsyncHttpClient.HEADER_ACCEPT_ENCODING, AsyncHttpClient.ENCODING_GZIP);
}
CookieHandler cookieHandler = this.client.getCookieHandler();
if (cookieHandler != null) {
OkHeaders.addCookies(result, cookieHandler.get(request.uri(), OkHeaders.toMultimap
(result.build().headers(), null)));
}
if (request.header(Network.USER_AGENT) == null) {
result.header(Network.USER_AGENT, Version.userAgent());
}
return result.build();
}