- 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(); } }
- 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(); } }
-
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(); }
-
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) { } });
-