How to pass a JSON array as a parameter in URL

前端 未结 9 1250
一整个雨季
一整个雨季 2020-11-28 10:35

I have an requirement to pass a some values from mobile to server in a web service call and so I am planning to pass all the values in JSON format like the below

<         


        
9条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-28 10:52

    You can pass your json Input as a POST request along with authorization header in this way

    public static JSONObject getHttpConn(String json){
            JSONObject jsonObject=null;
            try {
                HttpPost httpPost=new HttpPost("http://google.com/");
                org.apache.http.client.HttpClient client = HttpClientBuilder.create().build();
                StringEntity stringEntity=new StringEntity("d="+json);
    
                httpPost.addHeader("content-type", "application/x-www-form-urlencoded");
                String authorization="test:test@123";
                String encodedAuth = "Basic " + Base64.encode(authorization.getBytes());        
                httpPost.addHeader("Authorization", security.get("Authorization"));
                httpPost.setEntity(stringEntity);
                HttpResponse reponse=client.execute(httpPost);
                InputStream inputStream=reponse.getEntity().getContent();
                String jsonResponse=IOUtils.toString(inputStream);
                jsonObject=JSONObject.fromObject(jsonResponse);
                } catch (UnsupportedEncodingException e) {
    
                e.printStackTrace();
            } catch (ClientProtocolException e) {
    
                e.printStackTrace();
            } catch (IOException e) {
    
                e.printStackTrace();
            }
            return jsonObject;
    
    
        }
    

    This Method will return a json response.In same way you can use GET method

提交回复
热议问题