android developing RESTful android apps

在网上看到的一篇讲如何在 android上开发 RESTful程序的文章,在这里做个保留。


http://dl.google.com/googleio/2010/android-developing-RESTful-android-apps.pdf

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Android可以使用HttpURLConnection类来发送RESTful请求,可以使用setRequestMethod()方法设置请求的类型,可以使用getInputStream()方法获取服务器返回的响应。 ### 回答2: Android进行RESTful请求的代码主要是通过使用HTTP请求库来实现,例如使用Android原生的HttpURLConnection类或者第三方库如OkHttp进行网络请求。 下面是一个使用OkHttp库进行GET请求的例子: ```java // 导入OkHttp库的依赖 implementation 'com.squareup.okhttp3:okhttp:4.9.1' // 在代码中进行GET请求 OkHttpClient client = new OkHttpClient(); // 创建一个Request对象,包含请求的URL Request request = new Request.Builder() .url("http://example.com/api/data") // 替换为实际的接口URL .build(); // 发送请求并获取响应 try { Response response = client.newCall(request).execute(); // 处理响应数据 if (response.isSuccessful()) { String responseData = response.body().string(); // 在这里对responseData进行处理 } else { // 请求失败处理 } } catch (IOException e) { e.printStackTrace(); } ``` 这是一个基本的GET请求的例子,你可以根据需要进行自定义请求参数、请求头、请求体等。 除了GET请求,还可以使用OkHttp进行POST、PUT、DELETE等HTTP方法的请求,通过构建不同的Request对象来实现不同的操作。希望这个例子对你有帮助! ### 回答3: Android进行RESTful请求的代码可以使用HttpClient或者Volley来实现。 1. 使用HttpClient的示例代码: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONException; import org.json.JSONObject; public class RestClient { public static JSONObject get(String url) { try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); HttpResponse httpResponse = httpClient.execute(httpGet); InputStream inputStream = httpResponse.getEntity().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; StringBuilder stringBuilder = new StringBuilder(); while ((line = reader.readLine()) != null) { stringBuilder.append(line); } inputStream.close(); JSONObject jsonObject = new JSONObject(stringBuilder.toString()); return jsonObject; } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } return null; } } ``` 使用示例: ```java JSONObject result = RestClient.get("http://api.example.com/data"); if (result != null) { // 处理返回的json数据 } else { // 请求失败处理 } ``` 2. 使用Volley的示例代码: ```java import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; import com.android.volley.toolbox.Volley; import org.json.JSONObject; public class RestClient { public static void get(String url) { RequestQueue queue = Volley.newRequestQueue(context); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { // 处理返回的json数据 } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // 请求失败处理 } }); queue.add(jsonObjectRequest); } } ``` 使用示例: ```java RestClient.get("http://api.example.com/data"); ``` 以上是Android进行RESTful请求的代码示例,可以根据具体需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值