android restful

Android RESTFUL请求:

删除 DEL:

  1. //创建一个http客户端  
  2. HttpClient client=new DefaultHttpClient();  
  3. //创建一个DELETE请求  
  4. HttpDelete httpDelete=new HttpDelete("http://www.store.com/product/1234");  
  5. //向服务器发送DELETE请求并获取服务器返回的结果,可能是删除成功,或者失败等信息  
  6. HttpResponse response=client.execute(httpDelete);  
修改 PUT:
  1. //创建一个http客户端  
  2. HttpClient client=new DefaultHttpClient();  
  3. //创建一个PUT请求  
  4. HttpPut httpPut=new HttpPut("http://www.store.com/product/1234");  
  5. //组装数据放到HttpEntity中发送到服务器  
  6. final List dataList = new ArrayList();  
  7. dataList.add(new BasicNameValuePair("price", "11.99"));  
  8. HttpEntity entity = new UrlEncodedFormEntity(dataList, "UTF-8");  
  9. httpPut.setEntity(entity);  
  10. //向服务器发送PUT请求并获取服务器返回的结果,可能是修改成功,或者失败等信息  
  11. HttpResponse response=client.execute(httpPut);
获取数据GET:
 
  1. //创建一个http客户端  
  2. HttpClient client=new DefaultHttpClient();  
  3. //创建一个GET请求  
  4. HttpGet httpGet=new HttpGet("http://www.store.com/products");  
  5. //向服务器发送请求并获取服务器返回的结果  
  6. HttpResponse response=client.execute(httpGet);  
  7. //返回的结果可能放到InputStream,http Header中等。  
  8. InputStream inputStream=response.getEntity().getContent();  
  9. Header[] headers=response.getAllHeaders();  
新增数据 POST:
  1. //创建一个http客户端  
  2. HttpClient client=new DefaultHttpClient();  
  3. //创建一个POST请求  
  4. HttpPost httpPost=new HttpPost("http://www.store.com/product");  
  5. //组装数据放到HttpEntity中发送到服务器  
  6. final List dataList = new ArrayList();  
  7. dataList.add(new BasicNameValuePair("productName", "cat"));  
  8. dataList.add(new BasicNameValuePair("price", "14.87"));  
  9. HttpEntity entity = new UrlEncodedFormEntity(dataList, "UTF-8");  
  10. httpPost.setEntity(entity);  
  11. //向服务器发送POST请求并获取服务器返回的结果,可能是增加成功返回商品ID,或者失败等信息  
  12. HttpResponse response=client.execute(httpPost);  
  13.  
IOS RESTFUL:
  • SVHTTPRequest


获取数据:


    [[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]; 
                          }];

你可能感兴趣的:(移动平台,基础框架)