@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; }
@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); }
@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; }
@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; }
public void sendSubscription(Subscription subscription) { String interest = subscription.getInterest(); InterestSubscriptionChange change = subscription.getChange(); JSONObject json = new JSONObject(); try { json.put("app_key", appKey); } catch (JSONException e) { Log.e(TAG, e.getMessage()); } StringEntity entity = new StringEntity(json.toString(), "UTF-8"); String url = options.buildNotificationURL("/clients/" + clientId + "/interests/" + interest); ResponseHandlerInterface handler = factory.newSubscriptionChangeHandler(subscription); AsyncHttpClient client = factory.newHttpClient(); switch (change) { case SUBSCRIBE: client.post(context, url, entity, "application/json", handler); break; case UNSUBSCRIBE: client.delete(context, url, entity, "application/json", handler); break; } }
public static void postKonke(Context context, String url, String json, ResponseHandlerInterface callback) { Header[] headers = new Header[3]; // Log.i("", "accesstoken:" + accessToken); headers[0] = new BasicHeader("Authorization", "Bearer " + getAccessToken(context)); headers[1] = new BasicHeader("Content-Type", "application/json"); headers[2] = new BasicHeader("User-Agent", "imgfornote"); try { StringEntity s = new StringEntity(json); s.setContentEncoding("UTF-8"); s.setContentType("application/json"); Unit.httpClient.post(context, url, headers, s, "application/json", callback); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } }
private HttpManager(HttpConfig config) { super(); CLIENT_HEADER_FIELD = ReflectUtil.getField(getClass(), "clientHeaderMap"); CLIENT_HEADER_FIELD.setAccessible(true); PARAMS_TO_ENTITY = ReflectUtil.getMethod(getClass(), "paramsToEntity", RequestParams.class, ResponseHandlerInterface.class); PARAMS_TO_ENTITY.setAccessible(true); ADD_ENTITY_TO_REQUEST_ENTITY = ReflectUtil.getMethod(getClass(), "addEntityToRequestBase", HttpEntityEnclosingRequestBase.class, HttpEntity.class); ADD_ENTITY_TO_REQUEST_ENTITY.setAccessible(true); this.mHttpConfig = config; final Map<String, String> headMap = config.getHeadMap(); if (headMap != null) { getHeaders().putAll(headMap); } }
public void postScc(String url, ResponseHandlerInterface responseHandler, Map<String, Object> pararms) { RequestParams params = new RequestParams(); if (pararms != null) { Iterator<Map.Entry<String, Object>> iter = pararms.entrySet() .iterator(); while (iter.hasNext()) { Map.Entry<String, Object> entry = (Map.Entry<String, Object>) iter .next(); final String key = entry.getKey(); final Object val = entry.getValue(); if (val instanceof String || val instanceof Integer || val instanceof Long || val instanceof Short) { params.put(key, String.valueOf(val)); } else { params.put(key, GsonProvider.getInstance().toJson(val)); } } } post(url, params, responseHandler); }
public void postScc(String url, ResponseHandlerInterface responseHandler, Object... objects) { RequestParams params = new RequestParams(); if (objects != null && objects.length != 0) { StringBuilder sb = new StringBuilder(); for (Object object : objects) { if (object instanceof String || object instanceof Integer || object instanceof Long || object instanceof Short) { sb.append(object); } else { sb.append(GsonProvider.getInstance().toJson(object)); } sb.append(","); } params.put("_p", sb.toString()); } post(url, params, responseHandler); }
@Nullable private static RequestHandle call(int method, Context context, String url, RequestParams params, ResponseHandlerInterface responseHandler) { if (NetWorkUtil.isNetWorkConnected(context)) { switch (method) { case METHOD_GET: if (params == null) { return getInstance().get(context, url, responseHandler); } else { return getInstance().get(context, url, params, responseHandler); } case METHOD_POST: return getInstance().post(context, url, params, responseHandler); default: return null; } } else { responseHandler.sendFailureMessage(FAILED_NO_NETWORK, null, null, new NetworkErrorException(MESSAGE_NO_NETWORK)); return null; } }
public void get(ResponseHandlerInterface callback) { if (callback == null) { DebugLog.e(TAG, "CALLBACK IS NULL"); return; } if (StringUtil.isEmpty(url)) { DebugLog.e(TAG, "URL IS NULL"); return; } /*if (params != null) { getClient().get(context, url, headers, params, callback); } else */ if (contentType != null) { getClient().get(context, url, entity, contentType, callback); } else { getClient().get(context, url, callback); } }
public void post(ResponseHandlerInterface callback) { if (callback == null) { DebugLog.e(TAG, "CALLBACK IS NULL"); return; } if (StringUtil.isEmpty(url)) { DebugLog.e(TAG, "URL IS NULL"); return; } if (params != null) { getClient().post(context, url, headers, params, contentType, callback); } else if (entity != null) { getClient().post(context, url, entity, contentType, callback); } else { getClient().post(url, callback); } DebugLog.e(TAG, url + "?" + params.toString()); }
@Override protected RequestHandle sendRequest( cz.msebera.android.httpclient.impl.client.DefaultHttpClient client, cz.msebera.android.httpclient.protocol.HttpContext httpContext, cz.msebera.android.httpclient.client.methods.HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler, Context context) { if (this.service != null && accessToken != null) { try { ScribeRequestAdapter adapter = new ScribeRequestAdapter(uriRequest); this.service.signRequest(accessToken, adapter); return super.sendRequest(client, httpContext, uriRequest, contentType, responseHandler, context); } catch (Exception e) { e.printStackTrace(); } } else if (accessToken == null) { throw new OAuthException("Cannot send unauthenticated requests for " + apiInstance.getClass().getSimpleName() + " client. Please attach an access token!"); } else { // service is null throw new OAuthException("Cannot send unauthenticated requests for undefined service. Please specify a valid api service!"); } return null; // Hopefully never reaches here }
@Override public RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) { Intent serviceCall = new Intent(this, ExampleIntentService.class); serviceCall.putExtra(ExampleIntentService.INTENT_URL, URL); startService(serviceCall); return null; }
@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; }
@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); }
@Override public RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) { if (fileSize > 0) { // Send a GET query when we know the size of the remote file. return client.get(this, URL, headers, null, responseHandler); } else { // Send a HEAD query to know the size of the remote file. return client.head(this, URL, headers, null, responseHandler); } }
public static void postKonke(Context context, String url, RequestParams params, ResponseHandlerInterface callback) { Header[] headers = new Header[1]; // Log.i("","accesstoken:"+accessToken); headers[0] = new BasicHeader("Authorization", "Bearer " + getAccessToken(context)); // httpClient.post(context,url,headers,params,"application/json",callback); httpClient.post(context, url, headers, params, null, callback); }
@Override public void onFailure(ResponseHandlerInterface instance, int statusCode, Header[] headers, String responseString, Throwable throwable) { if (instance instanceof TXResponseHandler) { TXResponseHandler handler = (TXResponseHandler) instance; EventBus.getDefault().post(new TXResponseEvent(handler.getRequestTag(), statusCode, "failure", throwable)); } }
public AutoHttpRequest(AbstractHttpClient client, HttpContext context, HttpUriRequest request, ResponseHandlerInterface responseHandler, HttpManager.HttpConfig httpConfig) { super(client, context, request, responseHandler); this.responseHandler = responseHandler; this.mHttpConfig = httpConfig; IS_FINISH_FIELD = ReflectUtil.getField(getClass(), "isFinished"); IS_FINISH_FIELD.setAccessible(true); MAKE_REQUEST_WITH_RETRIES_ENTITY = ReflectUtil.getMethod(getClass(), "makeRequestWithRetries"); MAKE_REQUEST_WITH_RETRIES_ENTITY.setAccessible(true); }