2011-03-28 55 views
1

我需要發送JSON數組經由從機器人交方法的paramater ...和接收jsonarray作爲響應交參數..發送JSON數組從機器人

應該是什麼在Java下面提到請求的轉化??

curl -H "X-OpenSRF-service: open-ils.search" --data 'osrf-msg=[{"__p" : {"threadTrace" : 0, "payload" : { "__c" : "osrfMethod","__p" : { "params" :"30007004981493","method" : "open-ils.search.biblio.find_by_barcode"}},"type" : "REQUEST","locale" : "en-US"},"__c" : "osrfMessage"} ]' http://localhost/osrf-http-translator 

我已經做了這樣的..

HttpParams httpParams = new BasicHttpParams(); 

    HttpClient client = new DefaultHttpClient(httpParams); 

    HttpPost httpost = new HttpPost("http://"+hostname+"/osrf-http-translator"); 
    // httpost.setHeader("Accept", "application/json"); 
// httpost.setHeader("Content-type", "application/json"); 
    httpost.setHeader("X-OpenSRF-service", "open-ils.search"); 
    System.out.println("2"); 
    JSONObject data = new JSONObject(); 
    JSONObject _p = new JSONObject(); 
    JSONObject _p1 = new JSONObject(); 
    JSONObject osrfmsg = new JSONObject(); 
    HttpResponse response = null; 
    try { 
     _p.put("params",bookid);//"30007004981493" 
     _p.put("method","open-ils.search.biblio.find_by_barcode"); 
     JSONObject payload = new JSONObject(); 
     payload.put("_c", "osrfMethod"); 
     payload.put("_p", _p); 
     _p1.put("threadTrace",0); 
     _p1.put("payload", payload); 
     _p1.put("locale","en-US"); 
     _p1.put("type", "REQUEST"); 
     osrfmsg.put("_c","osrfMessage"); 
     osrfmsg.put("_p",_p1); 
     data.put("osrf-msg",osrfmsg); 


    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

      JSONArray osrfmsg2=new JSONArray(); 
    osrfmsg2.put(osrfmsg); 

     httpost.getParams().setParameter("osrf-msg",osrfmsg2); 
    response = client.execute(httpost); 
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); 

StringBuilder builder = new StringBuilder(); 
     for (String line = null; (line = reader.readLine()) != null;) 
     {  builder.append(line).append("\n"); } 

     JSONTokener tokener = new JSONTokener(builder.toString()); 
     JSONArray finalResult = new JSONArray(tokener); 

,但我沒能獲得JSON數組...

還有沒有其他的方法?

回答

0

那麼你正在構建一個JSONObject作爲參數,

嘗試使用

JSONArray作爲有效載荷的對象。

JSONArray payload = new JSONArray(); 

並與集合工作。 希望有所幫助。 您發送的上下文類型不是JSON,它是編碼形式

+0

他在哪裏? @chirag? – gumuruh 2012-04-18 09:29:37