Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在安卓系统中有推荐的post请求方式吗?_Android_Http Post - Fatal编程技术网

Android 在安卓系统中有推荐的post请求方式吗?

Android 在安卓系统中有推荐的post请求方式吗?,android,http-post,Android,Http Post,在安卓系统中有推荐的post请求方式吗?因为以前我使用HttpPost和HttpClient来执行post请求,但是这些类现在在API级别22中不推荐使用。您可以使用 然后将流转换为字符串 private String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = n

在安卓系统中有推荐的post请求方式吗?因为以前我使用
HttpPost
HttpClient
来执行post请求,但是这些类现在在API级别22中不推荐使用。

您可以使用

然后将流转换为字符串

 private String convertStreamToString(InputStream is) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line).append('\n');
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}
创建JSON

JSONObject j=new JSONObject();
        try {
            j.put("name","hello");
            j.put("email","hello@gmail.com");
        } catch (JSONException e) {
            e.printStackTrace();
        }

  //Call the method
        makeRequest("www.url.com",j.toString());

是的,他们不赞成。你可以使用谷歌开发者推荐的

截击有以下好处: 网络请求的自动调度。 多个并发网络连接。 具有标准HTTP缓存一致性的透明磁盘和内存响应缓存。 支持请求优先级划分。 取消请求API。您可以取消单个请求,也可以设置要取消的请求块或范围。 易于定制,例如重试和退避。 强大的排序功能,可以轻松地使用从网络异步获取的数据正确填充UI

截击非常容易使用:

// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
        new Response.Listener<String>() {
@Override
public void onResponse(String response) {
    // Display the first 500 characters of the response string.
    mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
    mTextView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
//实例化RequestQueue。
RequestQueue=Volley.newRequestQueue(this);
字符串url=”http://www.google.com";
//从提供的URL请求字符串响应。
StringRequest StringRequest=新的StringRequest(Request.Method.GET,url,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
//显示响应字符串的前500个字符。
mTextView.setText(“响应为:”+Response.substring(0500));
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
setText(“那没用!”);
}
});
//将请求添加到RequestQueue。
添加(stringRequest);

仅展示另一个可能对您有所帮助的库:

以他们网站上的一篇文章为例:

public static final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");

OkHttpClient client = new OkHttpClient();

String post(String url, String json) throws IOException {
  RequestBody body = RequestBody.create(JSON, json);
  Request request = new Request.Builder()
      .url(url)
      .post(body)
      .build();
  Response response = client.newCall(request).execute();
  return response.body().string();
}
或者

基本上,您可以创建一个带有rest API注释和正在调用的参数的接口,并可以接收解析后的json模型,如下所示:

public interface MyService {
    @POST("/api")
    void createTask(@Body CustomObject o, Callback<CustomObject > cb);
}
公共接口MyService{
@POST(“/api”)
void createTask(@Body CustomObject o,回调cb);
}
你也可以把它们放在一起,这里有一个指南对我帮助很大:


尽管这不是google文档中推荐的官方方式,但这些库很不错,值得一看。

您可以使用Volley或UrlConnection manager
public interface MyService {
    @POST("/api")
    void createTask(@Body CustomObject o, Callback<CustomObject > cb);
}