zoukankan      html  css  js  c++  java
  • 最佳网络编程实践二

    HttpUtil.java

    
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    
    /**
     * Created by 王德强 on 2017/7/25.
     */
    
    public class HttpUtil{
     public static void sendOkHttpRequest(String address,okhttp3.Callback callback){
            OkHttpClient client = new OkHttpClient();
            Request request= new Request.Builder()
                    .url(address)
                    .build();
            client.newCall(request).enqueue(callback);
        }
    }

    我们在调用sendOhHttpRequest()方法的时候就可以这样写:

    HttpUtil.sendOkHttpRequest("http://www.baidu.com",new okhttp3.Callback) throws IOException{
    @Override
    public void onResponse(Call call,Response response){
    //得到服务器返回的具体内容
      String responseData = response.body().string();
    }
    @Override
    public void onFailure(Call call,IOException e){
     //在这里对异常进行处理
    }
    }
  • 相关阅读:
    shell cut
    查询表名
    RandomAccessFile
    eclipse 背景颜色
    JAVA 获取分行符
    date time insert
    shell date time
    python
    gdg shell
    shell入门之变量测试
  • 原文地址:https://www.cnblogs.com/CCCrunner/p/11781927.html
Copyright © 2011-2022 走看看