从 android 发送 json 数组作为 post 参数

发布于 2024-10-27 04:33:45 字数 2327 浏览 9 评论 0原文

我需要通过 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 数组...

还有其他方法吗?

I need to send json array as a paramater via post method from android ...and receive jsonarray as response..

what should be the conversion of below mentioned request in 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

i have done it like this..

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

but i'm not able to get the json array...

is there any other method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丘比特射中我 2024-11-03 04:33:45

好吧,您正在构造一个 JSONObject 作为参数,

请尝试使用

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

 JSONArray payload = new JSONArray();

并与集合一起工作。
希望有帮助。
另外,您发送的上下文类型不是 JSON,而是表单编码的

Well you are constructing a JSONObject as the parameter,

try using

JSONArray as your payload object.

 JSONArray payload = new JSONArray();

And work with a collection.
Hope that helps.
Also your context type that you send is not JSON, it is form encoded

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文