第一行代码-常见网络工具基本使用

  1. webView
    settings = webview.getSettings();
    settings.setJavaScriptEnabled(true);
    webview.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    super.onPageStarted(view, url, favicon);
                    pb.setVisibility(View.VISIBLE);
                }
    
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                    //所有跳转强制在当前页面跳转,不跳游览器
                    webview.loadUrl(url);
                    return true;
                }
    
                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                    pb.setVisibility(View.INVISIBLE);
                }
            });
    
    webview.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onReceivedTitle(WebView view, String title) {
                    super.onReceivedTitle(view, title);
                }
    
                @Override
                public void onProgressChanged(WebView view, int newProgress) {
                    super.onProgressChanged(view, newProgress);
                }
            });
    
            webview.loadUrl(url);
    
     @Override
        public void onBackPressed() {
            if (webview.canGoBack()) {
                webview.goBack();
            } else {
                finish();
            }
        }
    

     

  2. HttpUrlConnection
    URL url = new URL(urlSpec);
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
    
        try {
            ByteArrayOutputStream out  = new ByteArrayOutputStream();
            InputStream in = connection.getInputStream();
    
            if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                throw new IOException(connection.getResponseMessage()+":with" +urlSpec);
            }
            int bytesRead = 0;
            byte[] buffer = new byte[1024];
            while ((bytesRead = in.read(buffer))>0) {
                out.write(buffer, 0, bytesRead);
            }
            out.close();
            return out.toByteArray();
        } finally {
            connection.disconnect();
        }
    }
  3. Okhttp

    implementation 'com.squareup.okhttp3:okhttp:3.11.0'
    
    public void Ok(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //get
                    OkHttpClient client=new OkHttpClient();
                    Request request=new Request.Builder()
                            .url("http://www.baidu.com")
                            .build();
                    Response response=client.newCall(request).execute();
                    String responseData=response.body().string();
    
                    //post
                    RequestBody body=new FormBody.Builder()
                            .add("username","scy")
                            .add("password","123")
                            .build();
                    Request request1=new Request.Builder()
                            .post(body)
                            .url("http://wwww.baidu.com")
                            .build();
                    String responseData1=client.newCall(request).execute().body().string();
                    System.out.println(responseData);
                    System.out.println(responseData1);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
     }

     

  4. Retrofit

    implementation 'com.squareup.okhttp3:okhttp:3.12.1'
    implementation 'com.squareup.retrofit2:retrofit:2.5.0'
    
    public interface PhotoService {
    
        @GET("photos/photos_1.json")
        Call<ResponsePhotoData> getPhotoAnswers();
    
    }
    
    public class RetrofitClient {
    
        private static Retrofit retrofit = null;
    
        public static  Retrofit getClient(String baseUrl) {
            if (retrofit==null) {
                retrofit = new Retrofit.Builder()
                        .baseUrl(baseUrl)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
            }
            return retrofit;
        }
    }
    
    public static PhotoService getPhotoService() {
            return RetrofitClient.getClient(BASE_URL).create(PhotoService.class);
    }
    
    ApiUtils.getPhotoService().getPhotoAnswers().enqueue(new Callback<ResponsePhotoData>() {
                @Override
                public void onResponse(Call<ResponsePhotoData> call, Response<ResponsePhotoData> response) {
                    mResponsePhotoData = response.body();
                    setData();
                }
    
                @Override
                public void onFailure(Call<ResponsePhotoData> call, Throwable t) {
    
                }
            });
    
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值