8

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.

Stephane Landelle
  • 6,687
  • 2
  • 18
  • 28
DoberDog
  • 502
  • 2
  • 10
  • 26

2 Answers2

23

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()
noni
  • 2,877
  • 17
  • 18
  • AsyncHttpClient client = new AsyncHttpClient(); ByteArrayEntity entity = new ByteArrayEntity(jsonObject.toString().getBytes("UTF-8")); client.post(getApplicationContext(), URL_connect,entity, "application/json", new AsyncHttpResponseHandler() And I get null on the server, why? – DoberDog Jan 10 '13 at 19:14
  • And That's how i create the Json Structure: JSONObject m1 = new JSONObject(); JSONObject m2 = new JSONObject(); List l1 = new LinkedList(); m1.put("data1","2013-01-10"); m1.put("data2","234652"); m2.put("data1","2013-01-11"); m2.put("data2","234235"); l1.add(m1); l1.add(m2); jsonObject.put("data3", l1); jsonObject.put("data4", string1); jsonObject.put("data5", string2); – DoberDog Jan 10 '13 at 19:21
  • Mm its difficult reading that here, can you debug it? Logcat should tell you in which line and class is the Null Pointer Exception generating. – noni Jan 10 '13 at 19:36
  • Cannot pass boolean by this method. Boolean gets converted to string. – sahil shekhawat Apr 29 '16 at 06:07
1

We can build the Above json format using JsonObject class which is available with javax.json-1.0.2.jar,

JsonObject jo = Json.createObjectBuilder()
            .add("data", 2376845)
            .add("data2", 12545)
            .add("array",Json.createArrayBuilder()
                    .add(Json.createObjectBuilder().add("data3", "2013-01-10").add("data4", 23532).build())
                    .add(Json.createObjectBuilder().add("data3", "2013-01-11").add("data4", 523526).build()))
                    .build();

After building json format

        AsyncHttpClient client=new AsyncHttpClient();
        Request request = client.preparePost(your host URL).
        setHeader("Content-Type","application/json").
        setHeader("Content-Length", ""+jo.toString().length()).setHeader("Authorization","Basic fgfgfgfhfhtetet=").
        setBody(jo.toString()).build();
        ListenableFuture<Response> r = null;
        //ListenableFuture<Integer> f= null;
        try{
        r = client.executeRequest(request);
        System.out.println(r.get().getResponseBody());
        }catch(IOException e){

        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

if you required basic authentication you need to add a key which is combination of username:password encoded with base64 to header,if not leave it in this case i added it to header

mostly this will work's for you

Note:AsyncHttpClient,ListenableFuture classes are available in async-http-client-1.7.5.jar

fresher
  • 117
  • 9