SamRowley
SamRowley

Reputation: 3485

Trouble sending JSON Post Android

It has been a while since I programmed for Android and I have lost all my previous work which had the code in it I am having problems with. I am developing an app for both Android and iPhone which connect to the same server to download data. All is well in the iPhone version but on Android when I hit the server with the post data containing the method name I would like to to run on the server it seems that the data is not added to the request.

Why is the POST not working in this request for Android but does for the iPhone version of the app?

Here is the code I am using:

public static void makeRequest() throws Exception {
    Thread t = new Thread() {
        public void run() {
            Looper.prepare(); //For Preparing Message Pool for the child Thread
            HttpClient client = new DefaultHttpClient();
            HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);
            HttpResponse response;
            HttpEntity entity;
            JSONObject json = new JSONObject();

            try {
                HttpPost post = new HttpPost("http://divisi.co.uk/rest/requesthandler.php");
                json.put("method", "getEventListData");

                StringEntity se = new StringEntity(json.toString());  
                se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                post.setEntity(se);

                response = client.execute(post);

                entity = response.getEntity();

                String retSrc = EntityUtils.toString(entity); 

                JSONObject result = new JSONObject(retSrc); //Convert String to JSON Object
                if(result.getString("SC") == "200"){
                    JSONArray data = result.getJSONArray("data");
                }
                else{

                }

            } catch(Exception e) {
                e.printStackTrace();
            }

            Looper.loop(); //Loop in the message queue
        }
    };
    t.start();
}

The response I get mack from the server is:

{"data":{"scalar":""},"SC":405,"timestamp":1363788265}

Meaning the method name was not found, i.e. not posted in my request to the server.

Upvotes: 1

Views: 1446

Answers (5)

Jschools
Jschools

Reputation: 2768

Your code to set the POST body may be just fine. I think the problem may be with your web service. Try using something like Rested or curl to manually make the call to your server. I made exactly the same request you are making, including with and without the POST body, and I got the same response from your server:

{"data":{"scalar":""},"SC":405,"timestamp":1365704082}

Some things that may be tripping you up:

JSONObject result = new JSONObject(retSrc); //Convert String to JSON Object
if(result.getString("SC") == "200"){
    JSONArray data = result.getJSONArray("data");
}

Here, you are comparing the string "405" to "200" using ==, when you should first do a null check and then use .equals("200") instead. Or, use result.getInt("SC") == 200 since this is an integer type in your response JSON.

Also, the "data" entity from your server response is not actually coming back as a JSON array. You should use getJSONObject("data") instead.

Additionally, it's always a good idea to externalize your strings.

Here's how the code should look:

public static final String JSON_KEY_SC = "SC";
public static final String JSON_KEY_DATA = "data";

...

JSONObject result = new JSONObject(retSrc); //Convert String to JSON Object

String sc = result.getString(JSON_KEY_SC);
if (sc != null && sc.equals("200")) {
    JSONObject data = result.getJSONObject(JSON_KEY_DATA);
}
else {
    ...
}

Upvotes: 1

MrTristan
MrTristan

Reputation: 740

heres an example of how i do things like this:

HttpClient httpClient = new DefaultHttpClient();

HttpPost postRequest = new HttpPost("http://divisi.co.uk/rest/requesthandler.php");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart(new FormBodyPart("method", new StringBody("getEventListData")));
reqEntity.addPart(new FormBodyPart("NEED_A_KEY_HERE", new StringBody("" + json.toString())));

postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
JSONObject responseDict = new JSONObject(EntityUtils.toString(response.getEntity()));

Upvotes: 2

L. G.
L. G.

Reputation: 9761

If the server can't read your request try to remove:

se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

It will use the mime type defaults HTTP.PLAIN_TEXT_TYPE i.e. "text/plain".

I don't see any other possibility, if your code is the one you posted and not a more complicated input JSON object.

Upvotes: 1

HunkD
HunkD

Reputation: 76

use fiddler on your sever side. see if the http message is correct. it seems your sever side problem, can you show us your sever side code which receive and parse json.

Upvotes: 1

Rohit
Rohit

Reputation: 3408

allow this is your "http://divisi.co.uk/rest/requesthandler.php" page code, then in android you can use this... you don't allow post in your URL,

Upvotes: 1

Related Questions