Passing Array of objects as url parameters in request

别来无恙 提交于 2019-12-11 06:38:33

问题


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?


回答1:


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

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;

}

Alternatively you can use json as well.




回答2:


best solution is send http post request in json or xml format .



来源:https://stackoverflow.com/questions/13093628/passing-array-of-objects-as-url-parameters-in-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!