1

下面粘贴的代码(来自 android 代码片段)是一个典型示例,说明如何通过一些简单的参数进行 http post。客户端是安卓客户端。这是我在互联网上能找到的典型...

public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/api/TripLocker");

try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList&lt;NameValuePair&gt;(2);
    nameValuePairs.add(new BasicNameValuePair("id", "12345"));
    nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}

}

我需要传递更复杂的数据。我想做一个 http 帖子,传递一个 TripLocker_s 对象,其中可能包含 500 个 TriplegModel_s 对象。对象的定义粘贴在下面...

public class TripLegModel_s
{ 
    public string ourDirection;  //SE, SW, SSW, etc. 
    public double longitude;
    public double latitude;
    public double altitude;
    public DateTimeOffset TimeStamp;
    public double speed;
}

public class TripLocker_s
{
    public ObservableCollection<TripLegModel_s> BreadCrums = new ObservableCollection<TripLegModel_s>();
}

如何使用 HTTP POST 传递此对象?顺便说一句,该服务是一个 ASP.NET Web api 服务。谢谢,加里

4

1 回答 1

1

转换为/从 JSON。那是最好的办法。 http://code.google.com/p/google-gson/ 它快如闪电。

于 2012-09-21T22:21:53.853 回答