Hello guys I'm new to restful service.
My friend created REST backend with Spring.
When I post this URL --> http://smartcar.tobaconsulting.com:9999/api/v1/login with postman or angularjs http.post, it's fine.
You guys can check it out in postman by including this body
{ "username":"alvin", "password":"alvin" }
and set content type to JSON (application/json).
But when I code to Android, why it's not working and return 500 error code.
My friend said that I'm not including header. I'm using loopj http library http://loopj.com/. Here is my android code
RequestParams params = new RequestParams();
params.put("username", username);
params.put("password", password);
AsyncHttpClient client = new AsyncHttpClient();
client.post("http://smartcar.tobaconsulting.com:9999/api/v1/login", params, new AsyncHttpResponseHandler() {
#Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
Log.d(TAG, String.format("status code: %d", statusCode));
}
#Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Log.d(TAG, String.format("status code: %d", statusCode));
}
});
Please help me guys. I trying to solve this problem for hours and haven't find any clue.
Thanks
i checked your URL no error in server response
Access-Control-Allow-Credentials →true
Access-Control-Allow-Methods →GET, POST, PUT, DELETE
Access-Control-Allow-Origin →chrome-extension://mkhojklkhkdaghjjfdnphfphiaiohkef
Access-Control-Expose-Headers →X-AUTH-TOKEN
Content-Length →0
Date →Thu, 07 Apr 2016 08:35:26 GMT
Server →Apache-Coyote/1.1
X-AUTH-TOKEN
→eyJ1c2VybmFtZSI6ImFsdmluIiwiZW1haWwiOiJhbHZpbkBhbHZpbi5jb20iLCJleHBpcmVzIjoxNDYwODgyMTI2MDc4LCJvd25lcmlkIjo2LCJteVJvbGVzIjpbIlVTRVIiLCJBRE1JTiJdfQ==.M9/71trIPbnXxCh7avSWBK42UDXUxWYXZrNOlHhO7iQ=
I would personally suggest using Volley lib for android, there is some useful method inside volley and google strongly recommended that
Transmitting Network Data Using Volley
Related
I am trying to get make a post request in Android using AsyncHttpClient using this code:
JSONObject jsonParams=new JSONObject();
try {
jsonParams.put("email", email);
}
catch (Exception e){
}
try {
StringEntity entity = new StringEntity(jsonParams.toString());
AsyncHttpClient client = new AsyncHttpClient();
client.post(getApplicationContext(),URL,entity, "application/json", new AsyncHttpResponseHandler(){
#Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {}
#Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {}
});
I am trying to access this data sent in the post request in my Python server. However, whenever I do request.json in my Python server, I get 'None' as response.
I also tried request.get_json(force=True) but getting 404 error code.
I dont really know if the data is being sent by the client APP or I am making a mistake while trying to retrieve it at the server side. Please help me with this issue.
String content = new String(responseBody);
Log.e("SUCCESS_RESP",""+content);
I'm using LoopJ AndroidAsyncHttp to post async Http calls from my android app. When i specify some RequestParams, the call keep failing, with a statusCode 415(Unsupported Media Type). As soon as i remove the requestParams the call goes through, without any error.
final AsyncHttpClient client;
String url = "http://someURL.com/SomeUserGUID/Profile/Statistics/Setup";
client = new AsyncHttpClient();
final RequestParams params = new RequestParams();
params.put("FirstClub", "false");
params.put("FairwayHit", "false");
Header[] headers = {
new BasicHeader("Accept-Language", Locale.getDefault().toString())
,new BasicHeader("Authorization", ApplicationObject.companyBasicAuthString)
,new BasicHeader("Accept", "application/json")
};
client.setTimeout(60000);
client.post( ProfileActivity.this,url, headers, params, "application/json", new AsyncHttpResponseHandler() {
#Override
public void onStart() {
//Do something before start
super.onStart();
}
#Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
//some code
}
#Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
//some code
}
});
My goal is for the URL to look like this: String url = http://someURL.com/SomeUserGUID/Profile/Statistics/Setup?FirstClub=false& FairwayHit=false
I would pref not to hard code the URL into one single string(This method works btw), because i have some calls that would create a really long URL string.
So how can i achieve a successful post call, with params, using AndroidAsyncHttp?
415 Unsupported Media Type
The server is refusing to service the request because the entity of the request is in a format not supported
by the requested resource for the requested method.
You're sending the Content-Type header (in your post call) as "application/json", but the params you've added are not JSON. This is probably why the error occurs.
I'm using this library to request from my web services. It didn't response at all in onSuccess and onFailure sometimes (these are the only two methods I overrides). I tested under 1.4.4 and 1.4.5 (android-async-http-1.4.5-20131110.125018-1, this one is better, but still encounter the problem sometimes). I'm sure it's not the network problem, because my ios app never encounter this problem from the same web services. And I can get response when I refresh immediately after this problem occurs.
Here is my code:
In requester.java
public class Requester
{
public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
AsyncHttpClient client = newClient();
client.get(getAbsoluteUrl(url), params, responseHandler);
}
public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
AsyncHttpClient client = newClient();
client.post(getAbsoluteUrl(url), params, responseHandler);
}
private static String getAbsoluteUrl(String relativeUrl) {
return Settings.BASE_URL + relativeUrl;
}
private static AsyncHttpClient newClient()
{
AsyncHttpClient client = new AsyncHttpClient();
client.setMaxRetriesAndTimeout(Settings.HTTP_TIMEOUT,Settings.HTTP_RETRIES);
return client;
}
}
In my activity who's making http request:
Requester.get(urlRequest, null, new JsonHttpResponseHandler()
{
#Override
public void onSuccess(int statusCode, org.apache.http.Header[] headers, org.json.JSONArray objects)
{
Logger.logDebug(TAG, "request success for " + " " + objects.length() + " objects");
}
#Override
public void onFailure(int statusCode, org.apache.http.Header[] headers, java.lang.Throwable throwable, org.json.JSONArray errorResponse)
{
Logger.logError(TAG,"Failed to request");
}
});
I'm using the similar source in a few projects. But all have the same problem. I don't know it's the problem of my code or the android-async-http library. Can anybody help? Thanks.
By the way, I'm normally making 3 requests at the same time by using the same method as the above mentioned source code but with different url.
I confirmed that the problem is related to the multiple requests. My solution is to replace with another library: Volley library. Problem solved!
I encounter this problem as well and upon further investigation I realized that if the json response received is not in proper json format or if the response from the server contains any extra characters you will not get a callback method being called, although the post or get would have been processed the callbacks will not be triggered. Check your response from the server to make sure.
I'm trying to upload a file to a django server from an android phone, and i'm getting a strange mimetype. My code utilizes the Asynchrnous HTTP request client, and I'm using the below code:
File file = new File(/*Get file*/)
RequestParams params = new RequestParams();
params.put("file", file);
getAsyncHttpClient().post(url, params, new JsonHttpResponseHandler() {
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
error.printStackTrace();
}
public void onSuccess(final JSONObject obj) {
Log.i(TAG, obj.toString());
}
});
The content-type I'm getting on the server from a django FileField is application/octet-stream. Is there anyway to get the real mimetype to be passed in? I have been researching on this for sometime.
I read these below links:
Uploading an image from Android (with Android Asynchronous Http Client) to rails server (with paperclip)
Android image file sent to php upload image file is application/octet-stream type and not image/jpeg?
Is there something else I should be passing into the Android Asynchronous Http Client?
Can't believe I didn't see this before.
http://loopj.com/android-async-http/doc/com/loopj/android/http/RequestParams.html
You can just do:
params.put("file", file, mimetype);
I'm using loopj to talk to a rest server which has different HTTP response codes for specific successful responses (i.e 200, 202). The AsyncHttpResponseHandler has an onSuccess(String content) callback but nothing that returns the HTTP response code. How can I get this?
Try
#Override
public void onSuccess(int statusCode, String content) {
}