Given a WebResourceRequest which is a post request and has body how to i make an http call and get the response?
I know this sounds pretty basic butI'm having a problem getting the answer
Have you tried looking at Volley. It is quite powerful and easy to use and gets all your needs handled.
After implementing it if you are still getting nothing try PostMan with the same parameters as you are using to make the http call. If PostMan fails to show data then parameters are wrong.
Volley - https://developer.android.com/training/volley/
PostMan - https://www.getpostman.com/
Related
When i call any webService with expired token,the service returns code 498 with error message "your tokken is expired"..BUT the retrofit gives the response code 500 with error message " internel server error".
response.code() //500 instead of 498
I tested the same url with postman and its working fine but the issue is only with retrofit.
I am using same services on IOS with AFNetworking and its also working fine there.
Any body can help to figure out why this is happening? thanks in advance.
This is not a full answer, but it's too big for a comment.
5xx codes are server errors, this means that the server is crashing, not the app nor is retrofit buggy. The issue most likely is in the server, but can be caused by retrofit - yes, that's true.
My experience with all the questions that say: "I tried this insert random network call here with postman and it works, but retrofit returns 500" is because postman adds headers by default, which retrofit doesn't. The server implementation then expects these headers to be set and due to a faulty implementation crashes if said headers are missing.
I would check the headers retrofit is sending and the headers postman is sending and compare both and make sure which one crashes the server.
It can also be that OkHttp (Retrofit uses OkHttp under the hood) is adding some headers which the server cannot cope with. This would be stranger to me, but not impossible. I think it adds for example by default gzip and some servers might not handle this correctly.
If you have access to the server, than it might even be worth checking there the logs. They might point you right away to the issue.
I'm sorry but I cannot point you directly to the problem. These are just tips to get you started. Hope they help.
I am using asp.net webapi v2 to create a rest api, everything is OK ! the GET request handled well, but I have a problem with POST request. I write a POST method in the Controller, and can call it using Postman. But when I try to call POST request with retrofit in my android app, it fails and get me this error : 405 Method Not Allowed.
It seems that every thing is okey but I don't know whats the problem !
this is my retrofit rest service :
#FormUrlEncoded
#POST("/Report")
void sendReport(#Field("UniqueID") String UniqueID, #Field("ShopID") long ShopID, #Field("Content") String Content, Callback<Boolean> callback);
I should mention that I've return bool in my POST action ! can it cause the problem because of Boolean in the retrofit ?!
what should I do now ?
Finally I found the solution, but I don't know why this happened !
The problem was the URL endpoint, It seems that POST method in webapi are case sensitive and I should use report against Report !!! (That was not happen in GET cases)
Another problem was the www prefix ! when I remove that it works like a charm but again it doesn't matter in GET cases !!!
I post this answer to help someone with the similar case ... hope so.
I am having the following API call:
http://rollout.gr/api/?query={%22search%22:%22places%22,%22page%22:1}
This API call is executed correctly in my browser. But when I use a DefaultHttpClient to execute this url in my Android application, I get a null response.
I suppose the problem is the JSON data in the HTTP url. Thus, I would like to ask which is the proper way to handle such url in an Android application?
Thanks a lot in advance!
The accolades aren't valid URL characters. The browser is userfriendly enough to automatically URL-encode them, but DefaultHttpClient isn't. The correct line to use from code is:
http://rollout.gr/api/?query=http://rollout.gr/api/?query=%7b%22search%22:%22places%22,%22page%22:1%7d
Note the encoding for the accolades (%7b, %7d).
Your problem may be the strictmode here.
I recommend to do http request in threads or asynctasks. strictmode doesnt let app do http reauest in uithread. maybe your console shows a warning and you get null from http response because of this.
This project may solve your problem:
http://loopj.com/android-async-http/
Not knowing your particular HTTP initialization code, I'm going to assume you didn't provide an explicit JSON accept header. A lot of REST endpoints require this.
httpget.setHeader("Accept", "application/json");
I made a test project just testing http request and response behavior, There is a simple http class extending Asynctask to make request and get response. I am performing some string manipulation on http response, there is a problem my code which is using http response is executing before getting the response from class, which results null pointer exception. So to resolve this i made a thread and all code dependent on http response put in that thread. Now every thing is working fine.But i want to know is there any other technique to handle this? or my approach is good? Please suggest.
i want to know is there any other technique to handle this?
Use AsyncTask for Android Http Request.
I am trying to intercept http POST method in WebView android, but not able to find any suitable method for the same. In API 11 there is a method shouldInterceptRequest, but it gives only webviewq and url as parameters so cater only GET request, it doesnot provide POST body data and request type indicator.
My question : Is there any way to override this method in android NDK ? or if i can pass a flag which identify request and also i can provide POST data.
Also if you have any other solution, tell me.