Passing Array of objects as url parameters in requ

2019-07-30 19:03发布

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?

2条回答
Deceive 欺骗
2楼-- · 2019-07-30 19:34

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.

查看更多
\"骚年 ilove
3楼-- · 2019-07-30 19:49

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

查看更多
登录 后发表回答