传递对象的数组作为在请求URL参数(Passing Array of objects as url

2019-10-17 08:10发布

我需要把对象的数组(每个对象有2个场)作为参数在HTTP请求的URL。 我怎样才能做到这一点,这个环节应该怎么样子?

Answer 1:

你可以用你的结构的XML即对象的数组,每个都具有两个字段,然后将其转换成字符串,作为一个例子,

       String  input = String.format("<Request><Data><Id>%s</Id></Data> 
       </Request>",studentIdSelected);

然后调用此方法输入和URL作为参数张贴您的数据,

       public static String retriver(String Url, String input) {

    String responseString = null;
    StringEntity stringEntity;
    HttpPost postRequest = new HttpPost(Url);
    try {

        Log.e("string is", input + "\n" + Url);
        stringEntity = new StringEntity(input, "UTF-8");
        stringEntity.setContentType("application/atom+xml");

        postRequest.setEntity(stringEntity);
        Log.v("Post", "Posted");
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(postRequest);  
        HttpEntity getResponseEntity = response.getEntity();

        responseString = EntityUtils.toString(getResponseEntity);

    } catch (Exception e) {
        // TODO: handle exception
        postRequest.abort();
        Log.w("HttpPostRetreiver", "Error for URL " + Url, e);
    }

    return responseString;

}

或者您可以使用JSON为好。



Answer 2:

最好的解决办法是JSON或XML格式发送HTTP POST请求。



文章来源: Passing Array of objects as url parameters in request