2013-01-10 20 views
8

Stavo usando la libreria LoopJ AndroidAsyncHttp per comunicare con il mio server PhP. E ho un problemaLoopJ AndroidAsyncHttp e JSON POST ARRAY

Ho bisogno di inviare un JsonObject del genere:

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

Ma nel javadoc; gli unici parametri sono RequestParams e non hanno alcun tipo di array. Qualcuno può aiutarmi? Oppure fammi vedere qualcosa che posso usare. Grazie.

+0

Partenza http://stackoverflow.com/questions/6218143/android- post-json-using-http –

risposta

23

Usa

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

invece di:

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

analizzare il vostro JSON come String:

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

Dove cliente è un AsyncHttpClient e bodyAsJson è un JSON all'interno di una stringa

yourJsonObj.toString() 
+0

AsyncHttpClient client = new AsyncHttpClient(); Entità ByteArrayEntity = new ByteArrayEntity (jsonObject.toString(). GetBytes ("UTF-8")); client.post (getApplicationContext(), URL_connect, entità, "application/json", nuova AsyncHttpResponseHandler() e ottengo nulla sul server, perché – DoberDog

+0

E così quelle che ho creare la struttura JSON:? JSONObject m1 = new JSONObject(); JSONObject m2 = new JSONObject(); \t \t \t \t \t \t \t \t Lista L1 = new LinkedList(); \t \t \t \t \t \t \t \t m1.put ("data1", "2013-01-10"); \t m1.put ("data2", "234652"); \t \t \t \t \t \t \t m2.put ("data1", "2013/01/11"); \t m2.mettere ("data2", "234235"); \t \t \t \t \t \t \t \t l1.add (M1); \t \t l1.add (m2); \t \t \t \t \t \t \t jsonObject.put ("data3", l1); jsonObject.put ("data4", stringa1); jsonObject.put ("data5", stringa2); – DoberDog

+0

Mm la sua lettura difficile che qui, puoi eseguire il debug? Logcat dovrebbe indicare in quale riga e classe viene generata l'eccezione Null Pointer. – noni

1

Possiamo costruire il formato JSON sopra utilizzando classe JsonObject che è disponibile con 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(); 

Dopo aver costruito formato JSON

 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(); 
     } 

se richiesto di base autenticazione è necessario aggiungere una chiave che è la combinazione di nome utente: password codificata con base64 all'intestazione, se non lasciarlo in questo caso l'ho aggiunto all'intestazione

per lo più questo sarà il lavoro per voi di

Nota: AsyncHttpClient, classi ListenableFuture sono disponibili in async-http-client-1.7.5.jar