Previous Android Response Being Added to Status Line - android

I'm using Volley to communicate with an API.
I am sending a request and on success of that request I immediately fire off another one, using the same message queue.
The issue I have is the second request is responding with the following error:
java.net.ProtocolException: Unexpected status line: {"id":47}HTTP/1.1 200 OK
The {"id":47} is the response body from the first request. I'm not even going near the status line in my code and the requests are fairly simple.
What on earth is happening?! Is it a bug within Volley?

I would suppose that the status line "HTTP/1.1 200 OK" is expected to be before the actual response content {"id":47} - according to the error message it seems to be exactly reversed.

In my case, the server return content when status code is 204,
api should not return data when 204 responses

Related

how to fix 500 response code error in Retrofit Android Studio

I am facing problem implementing the POST RETROFIT in openrouteservices maps to get round route.
Attached photo below is the error log.Enter image description here
This is my API Interface
Call<Round> getRoundCoordinates(
/*#Header("Content-Type: application/json") String authHeader ,*/
#Header("Authorization") String authHeader,
#Query("coordinates") List<List<Double>> coordinates,
#Query("length") int length,
#Query("seed") int seed
);
and 500 Error code is showing. Anyone who can help .Thanks in Advance :)
Error 500 is a server internal error. So either it cannot handle your parameter values or it is totally independent from client-side, the error lays in server code.
From the official document -
The 500 (Internal Server Error) status code indicates that the server
encountered an unexpected condition that prevented it from fulfilling
the request.
So you need to discuss with you backend team about it because it is an error occurred in the server, not your app (Client-side)

Android Retrofit + Rxjava: How to get response on non200 code?

This is how my request looks like:
ApiService apiService = retrofit.create(ApiService.class);
Observable<Response<UserUpdateResponse>> response = apiService.updateUser(Utils.getHeader(), object);
response.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::onSuccessUpdate,
this::onErr,
this::hideDialogLoading);
It's supposed to return 'code':'205' 'msg':'successfully update'. But when server response any code 201,202 (anything not 200) it will go to error.
Here is the Error.
java.net.ProtocolException: HTTP 205 had non-zero Content-Length: 121
So how do I prevent it from error, or how do I get error body? Thank you!.
HTTP response codes have a predefined definition and some have requirements that they must fullfill to be considered a valid HTTP payload. You cannot redefine what these codes mean for your application and expect well-implemented clients to accept it.
Looking specifically at HTTP 205 - Reset Content, which has the following requirement:
Since the 205 status code implies that no additional content will be provided, a server MUST NOT generate a payload in a 205 response.
Generally applications will just return HTTP 200 for all requests and include application-specific error codes in the payload. What you're doing does not make much sense.
So technically, I can get response 2xx. The problem was that server response body in response code 205 that suppose to be null (https://www.rfc-editor.org/rfc/rfc7231#section-6.3.6). So after set body null on server, android side works fine.

http status code even without error response retrofit

I'm working with Retrofit 1.9
I'm calling a webservice which never send response body, just a 200 when it's okey and a 404 when it's not.
To handle 202 success it's easy since the Retrofit callback already has a method "success" called when the code is between 200 and 300.
But the issue I have is with this part :
#Override
public void failure(RetrofitError error) {
error.getResponse().getStatus();
}
how should I get the http response code to check if it's a 404 or a 500(or whatever else) since the error.getResponse() is null ? I'm trying to do this to correctly notify the user.

Robospice caching when exception occured

I wan't to achieve next.
Depends on json content deside put or not to put data to Robospice Cache.
Sometimes data returned from the server is not valid. For example our authorization token goes off time. So we shouldn't cache this response.
But i can't find API which can help me to solve this trouble.
Here is how i am using requests now:
getSpiceManager().execute(getRequestCreator().getAllCategories(), getRequestCreator().getLastCacheKey(),
DurationInMillis.ONE_MINUTE * 120, new JSONCategoryListener(mCategories));
So the actual response is normal (status 200), but json content is telling me about exception.
So, what you want to get? It is right behavior for server. Server returns 200, means that request is successful. But it not guarantee, that wasn't some internal error of 'business logic' on server, such as not valid data or anything else.
EDITED
May be you can use your custom error handler:
class MyErrorHandler implements ErrorHandler {
#Override public Throwable handleError(RetrofitError cause) {
//check response on errors
}
}
And in createRestAdapterBuilder():
new RestAdapter.Builder()..setErrorHandler(new MyErrorHandler());
EDITED 2
You can implement in your robospice service method putDataInCache(Object cacheKey, T data), and in your listener check errors in content, and if no error then add it to cache, or remove it from cache

Android c2dm 302 Http error

everytime i want to send a push notification to my cell i get the 302 Moved Error from the Google servers. It says, it moved to www.google.com. This doesn't seem correct. So i have the following information:
I am sitting behind a proxy, but i tried this also without a proxy. No effect.
CUrl commandline method works fine
Receiving the auth token from the Google servers works fine
Changing the auth token i want to send has no real effect (changing the last 3-4 characters)
Sending no registration id has no effect
Here is the code, that sends a push notification:
QString headerStr = QString("GoogleLogin auth=") + m_authCode;
qDebug(qPrintable(headerStr));
QUrl url;
url.addQueryItem("registration_id", m_pRegCode->text());
url.addQueryItem("collapse_key", "0");
url.addQueryItem("data.message", "data");
qDebug("%s", qPrintable(url.toString()));
qDebug("%s", qPrintable(QString(url.encodedQuery())));
QByteArray data;
data = url.encodedQuery();
QUrl header("https://android.apis.google.com/c2dm/send");
QNetworkRequest req(header);
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded;charset=UTF-8");
req.setHeader(QNetworkRequest::ContentLengthHeader, data.length());
req.setRawHeader(QByteArray("Authorization"), headerStr.toAscii());
qDebug("%s", qPrintable(req.url().toString()));
m_pPushRep = m_pManager->post(req, data);
The answer i receive is:
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
here.
</BODY></HTML>
What am i doing wrong?
Edit: The solution was actually pretty simple: I had a whitespace at the end of my authentication id. This actually led to the errorcode. So for everyone who reads this in the far future: CHECK YOUR AUTHENTICATION CODE!
I ran into a similar issue. Worked from cURL but not from Qt. I however had to use the -k flag with cURL aka ignore ssl errors. After I did a simple ignoreSslErrors() on the QNetworkReply* it worked just fine for me, this might do the trick for you too. I now have this code to do the api call.
QNetworkRequest request(QUrl("https://android.apis.google.com/c2dm/send"));
QUrl postData;
postData.addQueryItem("collapse_key","1");
postData.addQueryItem("registration_id",id);
postData.addQueryItem("data.message",message);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded;charset=UTF-8");
request.setHeader(QNetworkRequest::ContentLengthHeader, postData.encodedQuery().length());
request.setRawHeader("Authorization","GoogleLogin auth=....");
network->post(request,postData.encodedQuery())->ignoreSslErrors();
Which works just fine for me.

Categories

Resources