【问题标题】:send json array as post parameter from android从android发送json数组作为post参数
【发布时间】:2011-03-28 11:32:55
【问题描述】:

我需要通过android的post方法发送json数组作为参数...并接收jsonarray作为响应..

下面提到的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 数组...

还有其他方法吗?

【问题讨论】:

    标签: java android curl httpclient


    【解决方案1】:

    你正在构造一个 JSONObject 作为参数,

    尝试使用

    JSONArray 作为您的有效负载对象。

     JSONArray payload = new JSONArray();
    

    并使用集合。 希望有帮助。 此外,您发送的上下文类型不是 JSON,而是格式编码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-02
      相关资源
      最近更新 更多