Android RESTFUL请求:
删除 DEL:
- //创建一个http客户端
- HttpClient client=new DefaultHttpClient();
- //创建一个DELETE请求
- HttpDelete httpDelete=new HttpDelete("http://www.store.com/product/1234");
- //向服务器发送DELETE请求并获取服务器返回的结果,可能是删除成功,或者失败等信息
- HttpResponse response=client.execute(httpDelete);
修改 PUT:
- //创建一个http客户端
- HttpClient client=new DefaultHttpClient();
- //创建一个PUT请求
- HttpPut httpPut=new HttpPut("http://www.store.com/product/1234");
- //组装数据放到HttpEntity中发送到服务器
- final List dataList = new ArrayList();
- dataList.add(new BasicNameValuePair("price", "11.99"));
- HttpEntity entity = new UrlEncodedFormEntity(dataList, "UTF-8");
- httpPut.setEntity(entity);
- //向服务器发送PUT请求并获取服务器返回的结果,可能是修改成功,或者失败等信息
- HttpResponse response=client.execute(httpPut);
获取数据GET:
- //创建一个http客户端
- HttpClient client=new DefaultHttpClient();
- //创建一个GET请求
- HttpGet httpGet=new HttpGet("http://www.store.com/products");
- //向服务器发送请求并获取服务器返回的结果
- HttpResponse response=client.execute(httpGet);
- //返回的结果可能放到InputStream,http Header中等。
- InputStream inputStream=response.getEntity().getContent();
- Header[] headers=response.getAllHeaders();
新增数据 POST:
- //创建一个http客户端
- HttpClient client=new DefaultHttpClient();
- //创建一个POST请求
- HttpPost httpPost=new HttpPost("http://www.store.com/product");
- //组装数据放到HttpEntity中发送到服务器
- final List dataList = new ArrayList();
- dataList.add(new BasicNameValuePair("productName", "cat"));
- dataList.add(new BasicNameValuePair("price", "14.87"));
- HttpEntity entity = new UrlEncodedFormEntity(dataList, "UTF-8");
- httpPost.setEntity(entity);
- //向服务器发送POST请求并获取服务器返回的结果,可能是增加成功返回商品ID,或者失败等信息
- HttpResponse response=client.execute(httpPost);
IOS RESTFUL:
[[SVHTTPClient sharedClient] setBasePath:@"http://api.twitter.com/1/"];
[[SVHTTPClient sharedClient] GET:@"users/profile_image"
parameters:[NSDictionary dictionaryWithObjectsAndKeys:
@"samvermette", @"screen_name",
@"original", @"size",
nil]
completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
imageCell.image = [[NSImage alloc] initWithData:response];
}];