传递对象数组作为请求的URL参数 [英] Passing Array of objects as url parameters in request

查看:2478
本文介绍了传递对象数组作为请求的URL参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to put an array of objects ( each object has 2 fields ) as parameters in url of http request. How can i do it and how should this link looks like?

推荐答案

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

You can make an xml with your structure i.e an array of objects each having two fields then convert that to string as, As an example,

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

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

Then call this method with input and url as parameters for posting your data,

       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也是如此。

Alternatively you can use json as well.

这篇关于传递对象数组作为请求的URL参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆