List of usage examples for com.squareup.okhttp OkHttpClient newCall
public Call newCall(Request request)
From source file:com.cuddlesoft.norilib.service.ServiceTypeDetectionService.java
@Override protected void onHandleIntent(Intent intent) { // Extract SearchClient.Settings from the received Intent. final Uri uri = Uri.parse(intent.getStringExtra(ENDPOINT_URL)); final Intent broadcastIntent = new Intent(ACTION_DONE); if (uri.getHost() == null || uri.getScheme() == null) { // The URL supplied is invalid. sendBroadcast(broadcastIntent.putExtra(RESULT_CODE, RESULT_FAIL_INVALID_URL)); return;/*from w w w . j a v a 2 s . co m*/ } // Create the HTTP client. final OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.setConnectTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS); okHttpClient.setReadTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS); // Iterate over supported URI schemes for given URL. for (String uriScheme : (TLS_SUPPORT.contains(uri.getHost()) ? URI_SCHEMES_PREFER_SSL : URI_SCHEMES)) { String baseUri = uriScheme + uri.getHost(); // Iterate over each endpoint path. for (Map.Entry<SearchClient.Settings.APIType, String> entry : API_ENDPOINT_PATHS.entrySet()) { // Create a HTTP request object. final Request request = new Request.Builder().url(baseUri + entry.getValue()).build(); try { // Fetch response. final Response response = okHttpClient.newCall(request).execute(); // Make sure the response code was OK and that the HTTP client wasn't redirected along the way. if (response.code() == HttpStatus.SC_OK && response.priorResponse() == null) { // Found an API endpoint. broadcastIntent.putExtra(RESULT_CODE, RESULT_OK); broadcastIntent.putExtra(ENDPOINT_URL, baseUri); broadcastIntent.putExtra(API_TYPE, entry.getKey().ordinal()); sendBroadcast(broadcastIntent); return; } } catch (IOException e) { // Network error. Notify the listeners and return. sendBroadcast(broadcastIntent.putExtra(RESULT_CODE, RESULT_FAIL_NETWORK)); return; } } } // End of the loop was reached without finding an API endpoint. Send error code to the BroadcastReceiver. sendBroadcast(broadcastIntent.putExtra(RESULT_CODE, RESULT_FAIL_NO_API)); }
From source file:com.datastore_android_sdk.okhttp.OkHttpStack.java
License:Open Source License
@Override public URLHttpResponse performRequest(Request<?> request, ArrayList<HttpParamsEntry> additionalHeaders) throws IOException { OkHttpClient client = mClient.clone(); int timeoutMs = request.getTimeoutMs(); client.setConnectTimeout(timeoutMs, TimeUnit.MILLISECONDS); client.setReadTimeout(timeoutMs, TimeUnit.MILLISECONDS); client.setWriteTimeout(timeoutMs, TimeUnit.MILLISECONDS); com.squareup.okhttp.Request.Builder okHttpRequestBuilder = new com.squareup.okhttp.Request.Builder(); okHttpRequestBuilder.url(request.getUrl()); for (final HttpParamsEntry entry : request.getHeaders()) { okHttpRequestBuilder.addHeader(entry.k, entry.v); }//from www . j a v a 2s . co m for (final HttpParamsEntry entry : additionalHeaders) { okHttpRequestBuilder.addHeader(entry.k, entry.v); } setConnectionParametersForRequest(okHttpRequestBuilder, request); com.squareup.okhttp.Request okHttpRequest = okHttpRequestBuilder.build(); Call okHttpCall = client.newCall(okHttpRequest); Response okHttpResponse = okHttpCall.execute(); return responseFromConnection(okHttpResponse); }
From source file:com.digitalglobe.iipfoundations.productservice.gbdx.GBDxAComp.java
public static String requestACompWorkflow(String input_directory, String output_directory, String authorization) {/*from w w w . ja va2 s. c o m*/ String workflow = generateACompRequestBody(input_directory, output_directory); System.out.println(workflow); MediaType mediaType = MediaType.parse("application/json"); RequestBody request_body = RequestBody.create(mediaType, workflow); //Properties gbdx = GBDxCredentialManager.getGBDxCredentials(); Request search_request = new Request.Builder().url("https://geobigdata.io/workflows/v1/workflows") .post(request_body).addHeader("content-type", "application/json") .addHeader("authorization", authorization).build(); OkHttpClient client = new OkHttpClient(); String id = ""; try { Response response = client.newCall(search_request).execute(); String body = response.body().string(); JSONObject obj = new JSONObject(body); id = obj.getString("id"); } catch (IOException e) { e.printStackTrace(System.out); System.exit(0); } return id; }
From source file:com.digitalglobe.iipfoundations.productservice.gbdx.GBDxCatalogQuery.java
public static void main(String[] args) { OkHttpClient client = new OkHttpClient(); client.setHostnameVerifier(new HostnameVerifier() { @Override//from w ww.ja v a 2 s . c o m public boolean verify(String hostname, SSLSession session) { return true; } }); Request request = new Request.Builder().url("https://geobigdata.io/catalog/v1/record/105041001281F200") .get().addHeader("content-type", "application/json") .addHeader("authorization", GBDxCredentialManager.getAuthorizationHeader()).build(); try { Response response = client.newCall(request).execute(); String body = response.body().string(); System.out.println(body); // PrintWriter out = new PrintWriter("catalog.json"); // out.println(body); // out.flush(); // out.close(); JSONObject obj = new JSONObject(body); String available = obj.getJSONObject("properties").getString("available"); System.out.println(available); // JSONArray arr = obj.getJSONArray("posts"); // for (int i = 0; i < arr.length(); i++) { // String post_id = arr.getJSONObject(i).getString("post_id"); // ...... // } MediaType mediaType = MediaType.parse("application/json"); RequestBody request_body = RequestBody.create(mediaType, "{ \n \"startDate\":null,\n \"endDate\": null,\n \"searchAreaWkt\":null,\n \"tagResults\": false,\n \"filters\": [\"identifier = '105041001281F200'\"],\n \"types\":[\"Acquisition\"]\n}"); Request search_request = new Request.Builder().url("https://alpha.geobigdata.io/catalog/v1/search") .post(request_body).addHeader("content-type", "application/json") .addHeader("authorization", GBDxCredentialManager.getAuthorizationHeader()).build(); response = client.newCall(search_request).execute(); body = response.body().string(); System.out.println(body); // PrintWriter out = new PrintWriter("search_result.json"); // out.println(body); // out.flush(); // out.close(); mediaType = MediaType.parse("application/json"); request_body = RequestBody.create(mediaType, "{\n \"rootRecordId\": \"105041001281F200\",\n \"maxdepth\": 2,\n \"direction\": \"both\",\n \"labels\": []\n }"); request = new Request.Builder().url("https://alpha.geobigdata.io/catalog/v1/traverse") .post(request_body).addHeader("content-type", "application/json") .addHeader("authorization", GBDxCredentialManager.getAuthorizationHeader()).build(); response = client.newCall(request).execute(); body = response.body().string(); System.out.println(body); PrintWriter out = new PrintWriter("traverse_result.json"); out.println(body); out.flush(); out.close(); } catch (IOException io) { System.out.println("ERROR!"); System.out.println(io.getMessage()); } }
From source file:com.digitalglobe.iipfoundations.productservice.gbdx.GBDxCredentialManager.java
public static Properties validateUserToken(String token) { Properties token_info = new Properties(); OkHttpClient client = new OkHttpClient(); client.setHostnameVerifier(new HostnameVerifier() { @Override//from ww w . j a va 2s. com public boolean verify(String hostname, SSLSession session) { return true; } }); Request request = new Request.Builder().url("https://geobigdata.io/auth/v1/validate_token").get() .addHeader("content-type", "application/json").addHeader("authorization", "Bearer " + token) .build(); try { Response response = client.newCall(request).execute(); String body = response.body().string(); token_info = parseResponse(body); } catch (IOException io) { System.out.println("ERROR!"); System.out.println(io.getMessage()); } return token_info; }
From source file:com.digitalglobe.iipfoundations.productservice.gbdx.GBDxEventNotification.java
public synchronized void notifyWorkflowComplete(String workflow_id) { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url("https://geobigdata.io/workflows/v1/workflows/" + workflow_id) .get().addHeader("content-type", "application/json") .addHeader("authorization", GBDxCredentialManager.getAuthorizationHeader()).build(); try {/*from w ww . ja v a2s. co m*/ // convert to thread, but for now use a loop and sleep for testing boolean isComplete = false; while (!isComplete) { Response response = client.newCall(request).execute(); String body = response.body().string(); System.out.println(body); JSONObject obj = new JSONObject(body); String state = obj.getJSONObject("state").getString("state"); System.out.println(state); if (state.equalsIgnoreCase("complete")) { isComplete = true; } else { try { Thread.sleep(5000); } catch (InterruptedException ex) { } } } } catch (IOException e) { System.out.println(e.getMessage()); System.exit(0); } }
From source file:com.digitalglobe.iipfoundations.productservice.gbdx.GBDxS3Credentials.java
public static Properties getS3Credentials(String auth_header) { Properties creds = new Properties(); OkHttpClient client = new OkHttpClient(); client.setHostnameVerifier(new HostnameVerifier() { @Override/* w w w . j a v a 2 s . c om*/ public boolean verify(String hostname, SSLSession session) { return true; } }); Request request = new Request.Builder().url("https://geobigdata.io/s3creds/v1/prefix").get() .addHeader("content-type", "application/json").addHeader("authorization", auth_header).build(); try { Response response = client.newCall(request).execute(); String body = response.body().string(); creds = parseResponse(body); } catch (IOException io) { System.out.println("ERROR!"); System.out.println(io.getMessage()); } return creds; }
From source file:com.digitalglobe.iipfoundations.productservice.gbdx.GBDxWorkflow.java
public static String status(String wf_id, String authorization) throws Exception { logger.trace("Entering Status - workflow id: {}", wf_id); String status = ""; Properties gbdx = GBDxCredentialManager.getGBDxCredentials(); Request workflow_status_request = new Request.Builder() .url("https://geobigdata.io/workflows/v1/workflows/" + wf_id).get() .addHeader("content-type", "application/json").addHeader("authorization", authorization).build(); OkHttpClient client = new OkHttpClient(); String body = null;/*from w w w.j a v a 2 s . c o m*/ try { Response response = client.newCall(workflow_status_request).execute(); body = response.body().string(); JSONObject json = new JSONObject(body); JSONObject state = json.getJSONObject("state"); status = state.getString("state"); } catch (JSONException e) { logger.warn("Unable to parse response for workflow id: {}, Error: {}, Body: {}", wf_id, e.getMessage(), body); } catch (IOException e) { logger.warn(e.getMessage()); } logger.trace("Leaving Status - status: {}", status); return status; }
From source file:com.digitalglobe.iipfoundations.productservice.orderservice.OrderService.java
/** * //from w w w.j a v a2s .co m * @param cat_id * @param auth_token * @return - the order_id * @throws IOException * @throws OrderServiceException */ public static String order1b(String cat_id, String auth_token) throws IOException, OrderServiceException { String request = generateOrder1bRequestBody(cat_id); System.out.println(request); MediaType mediaType = MediaType.parse("application/json"); RequestBody request_body = RequestBody.create(mediaType, request); //Properties gbdx = GBDxCredentialManager.getGBDxCredentials(); Request search_request = new Request.Builder().url("http://orders.iipfoundations.com/order/v1") .post(request_body).addHeader("content-type", "application/json") .addHeader("authorization", "Basic " + auth_token).addHeader("username", username) .addHeader("password", password).build(); OkHttpClient client = new OkHttpClient(); System.out.println(search_request.toString()); Response response = client.newCall(search_request).execute(); if (200 == response.code()) { String body = response.body().string(); System.out.println(body); JSONObject obj = new JSONObject(body); JSONArray orders = obj.getJSONArray("orders"); JSONObject order = orders.getJSONObject(0); int id = order.getInt("id"); return Integer.toString(id); } else { System.out.println(response.body().string()); logger.error(response.message()); throw new OrderServiceException(response.message()); } }
From source file:com.dreamfactory.kurtishu.pretty.view.task.DownloadImagTask.java
License:Apache License
private void downloadImg(String url) { Request request = new Request.Builder().url(url).build(); OkHttpClient okHttpClient = new OkHttpClient(); try {/*w ww .ja v a2 s .co m*/ Response response = okHttpClient.newCall(request).execute(); if (null != response && response.isSuccessful()) { String filePath = storeImgToSDCard(response.body().byteStream()); setPostMessage(EXECUTE_STATE_DOWNLOAD_SUCCESS, filePath); } else { setPostMessage(ExecutableThread.EXECUTE_STATE_FAILURE, "Internet error!"); } } catch (IOException e) { Logger.e("Error message: " + e.toString()); setPostMessage(ExecutableThread.EXECUTE_STATE_FAILURE, e.getMessage()); } }