List of usage examples for com.squareup.okhttp Response body
ResponseBody body
To view the source code for com.squareup.okhttp Response body.
Click Source Link
From source file:com.mallorcasoftware.openweatherclient.api.DefaultApi.java
License:Apache License
private com.squareup.okhttp.Call getForecastCall(String q, String id, String units, String lang, final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException { Object localVarPostBody = null; // create path and map variables String localVarPath = "/forecast".replaceAll("\\{format\\}", "json"); List<Pair> localVarQueryParams = new ArrayList<Pair>(); if (q != null) localVarQueryParams.addAll(apiClient.parameterToPairs("", "q", q)); if (id != null) localVarQueryParams.addAll(apiClient.parameterToPairs("", "id", id)); if (units != null) localVarQueryParams.addAll(apiClient.parameterToPairs("", "units", units)); if (lang != null) localVarQueryParams.addAll(apiClient.parameterToPairs("", "lang", lang)); Map<String, String> localVarHeaderParams = new HashMap<String, String>(); Map<String, Object> localVarFormParams = new HashMap<String, Object>(); final String[] localVarAccepts = { "application/json" }; final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts); if (localVarAccept != null) localVarHeaderParams.put("Accept", localVarAccept); final String[] localVarContentTypes = { "application/json" }; final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes); localVarHeaderParams.put("Content-Type", localVarContentType); if (progressListener != null) { apiClient.getHttpClient().networkInterceptors().add(new com.squareup.okhttp.Interceptor() { @Override//w ww. j a va 2 s. c o m public com.squareup.okhttp.Response intercept(com.squareup.okhttp.Interceptor.Chain chain) throws IOException { com.squareup.okhttp.Response originalResponse = chain.proceed(chain.request()); return originalResponse.newBuilder() .body(new ProgressResponseBody(originalResponse.body(), progressListener)).build(); } }); } String[] localVarAuthNames = new String[] { "UserSecurity" }; return apiClient.buildCall(localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarFormParams, localVarAuthNames, progressRequestListener); }
From source file:com.mamashai.pingxx.PingxxAndroidModule.java
License:Open Source License
@Kroll.method public void pay(Object arg) { HashMap<String, String> kd = (HashMap<String, String>) arg; OkHttpClient client = new OkHttpClient(); /*//from w w w.java 2s .co m RequestBody formBody = new FormEncodingBuilder() .add("order_no", kd.get("order_no")) .add("amount", kd.get("amount")) .add("channel", kd.get("channel")) .build(); Request request = new Request.Builder().url(url).build(); Response response = client.newCall(request).execute(); */ String url = kd.get("url") + "?order_no=" + kd.get("order_no") + "&amount=" + kd.get("amount") + "&channel=" + kd.get("channel"); Log.d(LCAT, "url :" + url); Request request = new Request.Builder().url(url).build(); try { Response response = client.newCall(request).execute(); if (response.code() >= 200 && response.code() <= 300) { String data = response.body().string(); Log.d(LCAT, "data:" + data); Intent intent = new Intent(); String packageName = _app.getAppCurrentActivity().getPackageName(); ComponentName componentName = new ComponentName(packageName, packageName + ".wxapi.WXPayEntryActivity"); intent.setComponent(componentName); intent.putExtra(PaymentActivity.EXTRA_CHARGE, data); TiActivitySupport support = (TiActivitySupport) TiApplication.getAppCurrentActivity(); support.launchActivityForResult(intent, REQUEST_CODE_PAYMENT, this); Log.d(LCAT, "after launch Activity"); } else { HashMap<String, Object> event = new HashMap<String, Object>(); event.put("code", "error"); event.put("text", "charge" + response); fireEvent("ping_paid", event); Log.d(LCAT, "request error"); } } catch (Exception e) { } }
From source file:com.markupartist.sthlmtraveling.provider.departure.DeparturesStore.java
License:Apache License
public Departures find(Context context, Site site) throws IllegalArgumentException, IOException { if (site == null) { Log.w(TAG, "Site is null"); throw new IllegalArgumentException(TAG + ", Site is null"); }// w w w.jav a 2 s .c o m Log.d(TAG, "About to get departures for " + site.getName()); String endpoint = apiEndpoint2() + "v1/departures/" + site.getId() + "?key=" + get(KEY) + "&timewindow=30"; HttpHelper httpHelper = HttpHelper.getInstance(context); Request request = httpHelper.createRequest(endpoint); OkHttpClient client = httpHelper.getClient(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) { Log.w(TAG, "A remote server error occurred when getting departures, status code: " + response.code()); throw new IOException("A remote server error occurred when getting departures."); } Departures departures; String rawContent = response.body().string(); try { departures = Departures.fromJson(new JSONObject(rawContent)); } catch (JSONException e) { Crashlytics.logException(e); Log.d(TAG, "Could not parse the departure reponse."); throw new IOException("Could not parse the response."); } return departures; }
From source file:com.markupartist.sthlmtraveling.provider.planner.Planner.java
License:Apache License
public Trip2 addIntermediateStops(final Context context, Trip2 trip, JourneyQuery query) throws IOException { Uri u = Uri.parse(apiEndpoint2());//from www . j a v a 2 s. co m Uri.Builder b = u.buildUpon(); b.appendEncodedPath("journey/v1/intermediate/"); b.appendQueryParameter("ident", query.ident); b.appendQueryParameter("seqnr", query.seqnr); int references = 0; String reference = null; for (SubTrip st : trip.subTrips) { if ((!TextUtils.isEmpty(st.reference)) && st.intermediateStop.isEmpty()) { b.appendQueryParameter("reference", st.reference); references++; reference = st.reference; } } u = b.build(); if (references == 0) { return trip; } HttpHelper httpHelper = HttpHelper.getInstance(context); com.squareup.okhttp.Response response = httpHelper.getClient() .newCall(httpHelper.createRequest(u.toString())).execute(); String rawContent; int statusCode = response.code(); switch (statusCode) { case 200: rawContent = response.body().string(); try { JSONObject baseResponse = new JSONObject(rawContent); if (baseResponse.has("stops")) { if (baseResponse.isNull("stops")) { Log.d(TAG, "stops was null, ignoring."); } else if (references == 1) { JSONArray intermediateStopsJson = baseResponse.getJSONArray("stops"); for (SubTrip st : trip.subTrips) { if (reference.equals(st.reference)) { for (int i = 0; i < intermediateStopsJson.length(); i++) { st.intermediateStop .add(IntermediateStop.fromJson(intermediateStopsJson.getJSONObject(i))); } } } } else { JSONObject intermediateStopsJson = baseResponse.getJSONObject("stops"); for (SubTrip st : trip.subTrips) { if (intermediateStopsJson.has(st.reference)) { JSONArray jsonArray = intermediateStopsJson.getJSONArray(st.reference); for (int i = 0; i < jsonArray.length(); i++) { st.intermediateStop.add(IntermediateStop.fromJson(jsonArray.getJSONObject(i))); } } } } } else { Log.w(TAG, "Invalid response when fetching intermediate stops."); } } catch (JSONException e) { Log.w(TAG, "Could not parse the response for intermediate stops."); } break; case 400: // Bad request rawContent = response.body().string(); try { BadResponse br = BadResponse.fromJson(new JSONObject(rawContent)); Log.e(TAG, "Invalid error response for intermediate stops: " + br.toString()); } catch (JSONException e) { Log.e(TAG, "Could not parse the error response for intermediate stops."); } default: Log.e(TAG, "Status code not OK from intermediate stops API, was " + statusCode); } return trip; }
From source file:com.markupartist.sthlmtraveling.provider.planner.Planner.java
License:Apache License
public SubTrip addIntermediateStops(Context context, SubTrip subTrip, JourneyQuery query) throws IOException { Uri u = Uri.parse(apiEndpoint2());/* www .j a v a2s .c om*/ Uri.Builder b = u.buildUpon(); b.appendEncodedPath("journey/v1/intermediate/"); b.appendQueryParameter("ident", query.ident); b.appendQueryParameter("seqnr", query.seqnr); b.appendQueryParameter("reference", subTrip.reference); u = b.build(); HttpHelper httpHelper = HttpHelper.getInstance(context); com.squareup.okhttp.Response response = httpHelper.getClient() .newCall(httpHelper.createRequest(u.toString())).execute(); String rawContent; int statusCode = response.code(); switch (statusCode) { case 200: rawContent = response.body().string(); try { JSONObject baseResponse = new JSONObject(rawContent); if (baseResponse.has("stops")) { JSONArray intermediateStopJsonArray = baseResponse.getJSONArray("stops"); for (int i = 0; i < intermediateStopJsonArray.length(); i++) { subTrip.intermediateStop .add(IntermediateStop.fromJson(intermediateStopJsonArray.getJSONObject(i))); } } else { Log.w(TAG, "Invalid response when fetching intermediate stops."); } } catch (JSONException e) { Log.w(TAG, "Could not parse the reponse for intermediate stops."); } break; case 400: rawContent = response.body().string(); try { BadResponse br = BadResponse.fromJson(new JSONObject(rawContent)); Log.e(TAG, "Invalid response for intermediate stops: " + br.toString()); } catch (JSONException e) { Log.e(TAG, "Could not parse the reponse for intermediate stops."); } default: Log.e(TAG, "Status code not OK from intermediate stops API, was " + statusCode); } return subTrip; }
From source file:com.markupartist.sthlmtraveling.provider.planner.Planner.java
License:Apache License
private Response doJourneyQuery(final Context context, JourneyQuery query, int scrollDirection) throws IOException, BadResponse { Uri u = Uri.parse(apiEndpoint2());/*ww w.j a va 2s .c om*/ Uri.Builder b = u.buildUpon(); b.appendEncodedPath("v1/journey/"); if (scrollDirection > -1) { b.appendQueryParameter("dir", String.valueOf(scrollDirection)); b.appendQueryParameter("ident", query.ident); b.appendQueryParameter("seq", query.seqnr); } else { if (shouldUseIdInQuery(query.origin)) { b.appendQueryParameter("origin", query.origin.getId()); } else if (query.origin.hasLocation()) { b.appendQueryParameter("origin", query.origin.getName()); b.appendQueryParameter("origin_latitude", String.valueOf(query.origin.getLocation().getLatitude())); b.appendQueryParameter("origin_longitude", String.valueOf(query.origin.getLocation().getLongitude())); } else { b.appendQueryParameter("origin", query.origin.getNameOrId()); } if (shouldUseIdInQuery(query.destination)) { b.appendQueryParameter("destination", query.destination.getId()); } else if (query.destination.hasLocation()) { b.appendQueryParameter("destination", query.destination.getName()); b.appendQueryParameter("destination_latitude", String.valueOf(query.destination.getLocation().getLatitude())); b.appendQueryParameter("destination_longitude", String.valueOf(query.destination.getLocation().getLongitude())); } else { b.appendQueryParameter("destination", query.destination.getNameOrId()); } for (String transportMode : query.transportModes) { b.appendQueryParameter("transport", transportMode); } if (query.time != null) { b.appendQueryParameter("date", DATE_FORMAT.format(query.time)); b.appendQueryParameter("time", TIME_FORMAT.format(query.time)); } if (!query.isTimeDeparture) { b.appendQueryParameter("arrival", "1"); } if (query.hasVia()) { if (query.via.getId() == null) { b.appendQueryParameter("via", query.via.getName()); } else { b.appendQueryParameter("via", String.valueOf(query.via.getId())); } } if (query.alternativeStops) { b.appendQueryParameter("alternative", "1"); } } b.appendQueryParameter("with_site_id", "1"); // Include intermediate stops. //b.appendQueryParameter("intermediate_stops", "1"); u = b.build(); //Log.e(TAG, "Query: " + query.toString()); //Log.e(TAG, "Query: " + u.toString()); HttpHelper httpHelper = HttpHelper.getInstance(context); com.squareup.okhttp.Response response = httpHelper.getClient() .newCall(httpHelper.createRequest(u.toString())).execute(); Response r = null; String rawContent; int statusCode = response.code(); switch (statusCode) { case 200: rawContent = response.body().string(); try { JSONObject baseResponse = new JSONObject(rawContent); if (baseResponse.has("journey")) { r = Response.fromJson(baseResponse.getJSONObject("journey")); } else { Log.w(TAG, "Invalid response"); } } catch (JSONException e) { Log.d(TAG, "Could not parse the reponse..."); throw new IOException("Could not parse the response."); } break; case 400: rawContent = response.body().string(); BadResponse br; try { br = BadResponse.fromJson(new JSONObject(rawContent)); } catch (JSONException e) { Log.d(TAG, "Could not parse the reponse..."); throw new IOException("Could not parse the response."); } throw br; default: Log.d(TAG, "Status code not OK from API, was " + statusCode); throw new IOException("A remote server error occurred when getting deviations."); } return r; }
From source file:com.massivcode.weatherinfoexam.utils.NetworkUtil.java
License:Apache License
/** * URL ?? //from w ww . j a va 2s. c o m * @param urlString * @return * @throws IOException */ public static String getDataFromUrl(String urlString) throws IOException { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url(urlString).build(); Response response = client.newCall(request).execute(); return response.body().string(); }
From source file:com.meyersj.tamalenow.location.UpdateLocationService.java
License:Open Source License
public void postLocation(Location location) { String date = Utils.dateFormat.format(new Date()); Log.d(TAG, date);/* w ww. j a v a 2 s.co m*/ RequestBody formBody = new FormEncodingBuilder().add("vendor_id", "test_vendor") .add("tstamp", Utils.dateFormat.format(new Date())) .add("lat", String.valueOf(location.getLatitude())) .add("lon", String.valueOf(location.getLongitude())).build(); Request request = new Request.Builder().url(app.getAPIBase() + "/" + Endpoints.LOCATION).post(formBody) .build(); httpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { Log.d(TAG, "failure"); } @Override public void onResponse(Response response) throws IOException { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); Log.d(TAG, response.body().string()); } }); }
From source file:com.microsoft.identity.internal.test.labapi.api.DefaultApi.java
/** * Build call for getTestConfiguration/* w ww . java 2 s. c om*/ * @param appName Name of an application (optional) * @param appId App ID (optional) * @param federationProvider Match string search for federation provider name (optional) * @param mfa Whether or not MFA is enabled for the user (optional) * @param mam Whether or not MAM policy has been applied to the user (optional) * @param mdm Whether or not MDM policy has been applied to the user (optional) * @param ca Whether or not conditional access is enabled? (Weird what does this mean) (optional) * @param mamca Whether or not conditional access requiring a MAM enabled client was applied? (Weird what does this mean) (optional) * @param mdmca Whether or not conditional access requiring a MDM enrolled device was applied? (Weird what does this mean) (optional) * @param license Find a user with a matching license (optional) * @param federated Find a federated tenant (optional) * @param isFederated Find a federated user (optional) * @param userType Find a user who is a member or a guest... by saying which you want (optional) * @param role Find a user who is a member of the specified role (optional) * @param external Find an external user (optional) * @param upn Find an a user by their UPN (optional) * @param progressListener Progress listener * @param progressRequestListener Progress request listener * @return Call to execute * @throws ApiException If fail to serialize the request body object */ public com.squareup.okhttp.Call getTestConfigurationCall(String appName, String appId, String federationProvider, Boolean mfa, Boolean mam, Boolean mdm, Boolean ca, Boolean mamca, Boolean mdmca, String license, Boolean federated, Boolean isFederated, String userType, String role, Boolean external, String upn, final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException { Object localVarPostBody = null; // create path and map variables String localVarPath = "/user"; List<Pair> localVarQueryParams = new ArrayList<Pair>(); List<Pair> localVarCollectionQueryParams = new ArrayList<Pair>(); if (appName != null) localVarQueryParams.addAll(apiClient.parameterToPair("AppName", appName)); if (appId != null) localVarQueryParams.addAll(apiClient.parameterToPair("AppId", appId)); if (federationProvider != null) localVarQueryParams.addAll(apiClient.parameterToPair("federationProvider", federationProvider)); if (mfa != null) localVarQueryParams.addAll(apiClient.parameterToPair("mfa", mfa)); if (mam != null) localVarQueryParams.addAll(apiClient.parameterToPair("mam", mam)); if (mdm != null) localVarQueryParams.addAll(apiClient.parameterToPair("mdm", mdm)); if (ca != null) localVarQueryParams.addAll(apiClient.parameterToPair("ca", ca)); if (mamca != null) localVarQueryParams.addAll(apiClient.parameterToPair("mamca", mamca)); if (mdmca != null) localVarQueryParams.addAll(apiClient.parameterToPair("mdmca", mdmca)); if (license != null) localVarQueryParams.addAll(apiClient.parameterToPair("license", license)); if (federated != null) localVarQueryParams.addAll(apiClient.parameterToPair("federated", federated)); if (isFederated != null) localVarQueryParams.addAll(apiClient.parameterToPair("isFederated", isFederated)); if (userType != null) localVarQueryParams.addAll(apiClient.parameterToPair("userType", userType)); if (role != null) localVarQueryParams.addAll(apiClient.parameterToPair("role", role)); if (external != null) localVarQueryParams.addAll(apiClient.parameterToPair("external", external)); if (upn != null) localVarQueryParams.addAll(apiClient.parameterToPair("upn", upn)); Map<String, String> localVarHeaderParams = new HashMap<String, String>(); Map<String, Object> localVarFormParams = new HashMap<String, Object>(); final String[] localVarAccepts = { "application/json" }; final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts); if (localVarAccept != null) localVarHeaderParams.put("Accept", localVarAccept); final String[] localVarContentTypes = { }; final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes); localVarHeaderParams.put("Content-Type", localVarContentType); if (progressListener != null) { apiClient.getHttpClient().networkInterceptors().add(new com.squareup.okhttp.Interceptor() { @Override public com.squareup.okhttp.Response intercept(com.squareup.okhttp.Interceptor.Chain chain) throws IOException { com.squareup.okhttp.Response originalResponse = chain.proceed(chain.request()); return originalResponse.newBuilder() .body(new ProgressResponseBody(originalResponse.body(), progressListener)).build(); } }); } String[] localVarAuthNames = new String[] {}; return apiClient.buildCall(localVarPath, "GET", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarFormParams, localVarAuthNames, progressRequestListener); }