2

我需要在 http 请求的 url 中放置一个对象数组(每个对象有 2 个字段)作为参数。我该怎么做?这个链接应该是什么样子?

4

2 回答 2

1

您可以使用您的结构创建一个 xml,即一个对象数组,每个对象有两个字段,然后将其转换为字符串,例如,

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

然后使用 input 和 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。

于 2012-10-26T20:25:12.260 回答
1

最好的解决方案是以 json 或 xml 格式发送 http post 请求。

于 2012-10-26T20:21:44.350 回答