How to add query parameters to a HTTP GET request by OkHttp?

For okhttp3: private static final OkHttpClient client = new OkHttpClient().newBuilder() .connectTimeout(10, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS) .build(); public static void get(String url, Map<String,String>params, Callback responseCallback) { HttpUrl.Builder httpBuilder = HttpUrl.parse(url).newBuilder(); if (params != null) { for(Map.Entry<String, String> param : params.entrySet()) { httpBuilder.addQueryParameter(param.getKey(),param.getValue()); } } Request request = new Request.Builder().url(httpBuilder.build()).build(); client.newCall(request).enqueue(responseCallback); }

Writing image to local server

A few things happening here: I assume you required fs/http, and set the dir variable 🙂 google.com redirects to www.google.com, so you’re saving the redirect response’s body, not the image the response is streamed. that means the ‘data’ event fires many times, not once. you have to save and join all the chunks together to … Read more

PHP cURL GET request and request’s body

The accepted answer is wrong. GET requests can indeed contain a body. This is the solution implemented by WordPress, as an example: curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, ‘GET’ ); curl_setopt( $ch, CURLOPT_POSTFIELDS, $body ); EDIT: To clarify, the initial curl_setopt is necessary in this instance, because libcurl will default the HTTP method to POST when using CURLOPT_POSTFIELDS … Read more

Standardized way to serialize JSON to query string?

URL-encode (https://en.wikipedia.org/wiki/Percent-encoding) your JSON text and put it into a single query string parameter. for example, if you want to pass {“val”: 1}: mysite.com/path?json=%7B%22val%22%3A%201%7D Note that if your JSON gets too long then you will run into a URL length limitation problem. In which case I would use POST with a body (yes, I know, … Read more

Is it wrong to return 202 “Accepted” in response to HTTP GET?

If it’s for a well-defined and -documented API, 202 sounds exactly right for what’s happening. If it’s for the public Internet, I would be too worried about client compatibility. I’ve seen so many if (status == 200) hard-coded…. In that case, I would return a 200. Also, the RFC makes no indication that using 202 … Read more

How to add parameters to a HTTP GET request in Android?

I use a List of NameValuePair and URLEncodedUtils to create the url string I want. protected String addLocationToUrl(String url){ if(!url.endsWith(“?”)) url += “?”; List<NameValuePair> params = new LinkedList<NameValuePair>(); if (lat != 0.0 && lon != 0.0){ params.add(new BasicNameValuePair(“lat”, String.valueOf(lat))); params.add(new BasicNameValuePair(“lon”, String.valueOf(lon))); } if (address != null && address.getPostalCode() != null) params.add(new BasicNameValuePair(“postalCode”, address.getPostalCode())); if … Read more