zoukankan      html  css  js  c++  java
  • android访问网络--okhttp

    import com.squareup.okhttp.MediaType;
    import com.squareup.okhttp.OkHttpClient;
    import com.squareup.okhttp.Request;
    import com.squareup.okhttp.RequestBody;
    import com.squareup.okhttp.Response;
    import java.io.IOException;

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


    public static String get(String url) throws IOException {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
    .url(url)
    .build();
    Response response = client.newCall(request).execute();
    return response.body().string();
    }
    public static String post(String url, String json) throws IOException {
    OkHttpClient client = new OkHttpClient();
    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();
    }
    }
  • 相关阅读:
    我来解数独(附delphi源码)
    jquery(三)
    jquery(二)
    jquery(一)
    前端之JS(五)
    前端之JS(四)
    前端之JS(三)
    前端之JS(二)
    前端之CSS(三)
    前端之CSS(二)
  • 原文地址:https://www.cnblogs.com/jake-ge/p/4721499.html
Copyright © 2011-2022 走看看