I am trying to create a request using Retrofit2. I created the request using a standard library:
path = "https://www.iii.com/?id="+id+"&data=";
query = "{\"name\":\""+name+"\",\"quantity\":20}";
Final link is:
link = path+URLEncoder.encode(query, "UTF-8");
I tried different Retrofit2 options, but I can't understand how to translate my link to Retrofit2 link using together path and query with url encoded?
You can parse GET query parameters to retrofit using this code:
#GET("https://www.iii.com")
Observable<ResponseBody> getSomething(
#Query("id") int id,
#Query("data") String data
);
Retrofit will build it for you. Just pass your variables (assuming you know how to call retrofit requests) and retrofit will url encode it for you. You can refer to this link: https://square.github.io/retrofit/2.x/retrofit/index.html?retrofit2/http/Query.html
Values are converted to strings using Retrofit.stringConverter(Type, Annotation[]) (or Object.toString(), if no matching string converter is installed) and then URL encoded. null values are ignored. Passing a List or array will result in a query parameter for each non-null item.
you can add anotation for that like below
#Headers("charset=UTF-8")
#GET("https://www.iii.com")
Observable<ResponseBody> getSomething(
#Query("id") int id,
#Query("data") String data
);
Related
I am using AWS Textract to OCR images and create a searchable PDF as outlined in this AWS blog post.
The basic request code looks like this:
AmazonTextractClientBuilder builder = AmazonTextractClientBuilder.standard();
DetectDocumentTextRequest request = new DetectDocumentTextRequest()
.withDocument(new Document()
.withBytes(imageBytes));
DetectDocumentTextResult result = client.detectDocumentText(request);
List<Block> blocks = result.getBlocks()
This works out great however I would also like to write out and keep the original response JSON that contains all the information on what was detected where etc.
Is there a way to get to the response JSON using the JAVA SDK?
AWS doesn't return the response JSON to you in raw form. The assumption may have been that it wouldn't be required once it has been converted to a DetectDocumentTextResult object.
You are able to convert the DetectDocumentTextResult object to JSON (example) which should provide identical values. Note that the variable names will not be identical (e.g.: DocumentMetadata vs documentMetadata) but the values of those variables will be the same.
I am getting the URL dynamically which has few placeholders. But i need to pass the parameters to the placeholders.
Below is the URL which i am getting dynamically
http://example.com/name/{name}/age/{age}
i need to pass parameters for name and age for the above url. How can we achieve that using java.
I guess you are looking Rest Client to call this URL and get response.
http://example.com/name/{name}/age/{age}
String resourceURL = "http://example.com/name";
ResponseEntity<String> response = restTemplate.getForEntity(resourceURL + "/swarit/age/20", String.class);
I would also suggest to do little research before posting question.
I am accessing an api .which looks like
http://XXX.XX.XXX.XXX/api/Gallery/getgallerylist?userid="17" .
I have tried the following annotation.
#POST("Gallery/getgallerylist")
Call<GalleryImagesResponse> getGalleryImages(#Query("userid") String userid);
But the url it requests is http://XXX.XX.XXX.XXX/api/Gallery/getgallerylist?userid=17
So that how can I add double quote to userid's value like userid="17".
You can pass the string as #Path to the URL like this.
#POST("http://XXX.XX.XXX.XXX/api/Gallery/getgallerylist?" + "{path}")
Call<ResponseBody> getGalleryImages(#Path("path") String path);
and pass userid=17 to this call, it will append to the URL. and your done
you should try this before hitting url
URLDecoder.decode(url,UTF8);
I am getting some JSON values from the server but I don't know if there will be a particular field or not.I need to validate based on the key .
One Type Of Response
Another Type Of Response
In AysncTask we can use "has" function but in Retrofit i am unable to find a solution .
Provide me a solution
Retrofit will parse all attributes that you specified in your model. If some attribute in your JSON does not exist Retrofit will set NULL as the value of that attribute.
Knowing this feature, the only thing that you need to do is something like:
if(myObject.getReviewerDetails() == null)
// do something
Happy code!
You can check key exist or not using jsonObject.has like following way,
JSONObject jsonObject=new JSONObject();
if(jsonObject.has("reviewer_details")){
//do process with data
}
Hi I'm trying to do a simple http get query via Retrofit.
My parameter has some special characters and it seems that the url encoding fails.
Original:
data=[out:json];node["name"~"Karlsruhe"]["place"~"city|village|town"];out body;
correct encoding should look like this:
data=%5Bout%3Ajson%5D%3Bnode%5B%22name%22~%22Karlsruhe%22%5D%5B%22place%22~%22city%7Cvillage%7Ctown%22%5D%3Bout%20body%3B
but Retrofit creates this:
data=[out:json];node[%22name%22~%22Karlsruhe%22][%22place%22~%22city|village|town%22];out%20body;
and this will fail with:
java.lang.IllegalStateException: not valid as a java.net.URI:
http://overpass.osm.rambler.ru/cgi/interpreter?data=[out:json];node[%22name%22~%22Karlsruhe%22][%22place%22~%22city|village|town%22];out%20body;
at com.squareup.okhttp.HttpUrl.uri(HttpUrl.java:336) at
com.squareup.okhttp.internal.http.RouteSelector.resetNextProxy(RouteSelector.java:135)
at
com.squareup.okhttp.internal.http.RouteSelector.(RouteSelector.java:71)
at
com.squareup.okhttp.internal.http.RouteSelector.get(RouteSelector.java:76)
at
com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:321)
at
com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:245)
at com.squareup.okhttp.Call.getResponse(Call.java:267) at
com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:224)
at
com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:195)
at com.squareup.okhttp.Call.execute(Call.java:79) at
retrofit.OkHttpCall.execute(OkHttpCall.java:112)
What can be done here to fix this encoding issue?
Thanks
I am not sure about what the root cause of the encoding error is, but you can work around it with the encoded parameter to the Query notation. Setting the parameter to true tells retrofit the parameter is already encoded, so do not encode again.
In your service interface, add encoded=true to your #Query annotation. Something like --
Call<ResponseBody> getResponse(#Query(value = "data", encoded = true) String data);
Then, encode the parameter yourself before sending to retrofit.
final String encodedData = URLEncoder.encode(data, "UTF-8");
Call<ResponseBody> result = service.getResponse(encodedData);