LoopJ AndroidAsyncHttp and JSON POST ARRAY

DoberDog picture DoberDog · Jan 10, 2013 · Viewed 14.3k times · Source

I was using LoopJ AndroidAsyncHttp library to comunicate with my PhP Server. And i got a problem.

I need to send a JsonObject like that:

{      "data": 2376845,      
       "data2": 12545,      
       "array": [{"data3": "2013-01-10",          
                     "data4": 23532        },       
                    {"data3": "2013-01-11",             
                     "data4": 523526   }]
}

But in the javadoc; the only parameters it's RequestParams, and don't have any kind of Array. Can AnyOne Help me? Or Show me something that i can use. Thanks.

Answer

noni picture noni · Jan 10, 2013

Use

public void post(Context context, String url, HttpEntity entity, String contentType, AsyncHttpResponseHandler responseHandler)

instead of:

public void post(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler)

Parse your JSON as a String:

ByteArrayEntity entity = new ByteArrayEntity(bodyAsJson.getBytes("UTF-8"));
client.post(context, newUrl, entity, "application/json", responseHandler);

Where client is an AsyncHttpClient and bodyAsJson is a JSON inside a String

yourJsonObj.toString()