<WebView
        android:id="@+id/webview01_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
WebView webView = findViewById(R.id.webview01_id);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://beyondcbz.xyz");

加上权限:

<uses-permission android:name="android.permission.INTERNET" />

使用HttpURLConnection

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/send_quest"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发送请求"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/response_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </ScrollView>

</LinearLayout>
        Button sendRequest = (Button)findViewById(R.id.send_quest);
        responseText = (TextView)findViewById(R.id.response_text);

        sendRequest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendRequestWithHttpURLConnection();
            }
        });
private void sendRequestWithHttpURLConnection(){
    new Thread(new Runnable() {
        @Override
        public void run() {
            HttpURLConnection connection = null;
            BufferedReader reader = null;
            try{
                URL url = new URL("https://beyondcbz.xyz");
                connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(8000);
                connection.setReadTimeout(8000);
                InputStream in = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(in));
                StringBuilder response = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null){
                    response.append(line);
                }
                showResponse(response.toString());
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                if (reader != null){
                    try{
                        reader.close();
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                }
                if (connection != null){
                    connection.disconnect();
                }
            }
        }
    }).start();
}

private void showResponse(final String response){
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            responseText.setText(response);
        }
    });
}

OkHttp


添加依赖:com.squareup.okhttp3:okhttp:3.4.1

具体方法可查看: https://beyondcbz.xyz/?p=228

private void sendRequestWithOKHttp(){
    new Thread(new Runnable() {
        @Override
        public void run() {
            try{
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url("https://beyondcbz.xyz")
                        .build();
                Response response = client.newCall(request).execute();
                String responseData = response.body().string();
                showResponse(responseData);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }).start();
}

private void showResponse(final String response){
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            responseText.setText(response);
        }
    });
}
public void onClick(View v) {
    sendRequestWithOKHttp();
}

共有 0 条评论