以下是一个简单的示例代码,演示如何在 Android Studio 中使用 Java 进行 POST 请求,并打印出响应信息:

import java.io.*;
import java.net.*;
import java.util.*;

public class PostRequest {
    public static void main(String[] args) {
        try {
            // 创建URL对象
            URL url = new URL('http://example.com/api');
            
            // 创建URLConnection对象
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod('POST');
            con.setDoOutput(true);
            
            // 创建请求参数
            Map<String, String> params = new HashMap<>();
            params.put('name', 'John');
            params.put('age', '30');
            
            // 将请求参数转换为字节数组
            byte[] postDataBytes = getPostDataBytes(params);
            
            // 设置请求头
            con.setRequestProperty('Content-Type', 'application/x-www-form-urlencoded');
            con.setRequestProperty('Content-Length', String.valueOf(postDataBytes.length));
            
            // 发送请求
            OutputStream out = con.getOutputStream();
            out.write(postDataBytes);
            out.flush();
            out.close();
            
            // 获取响应信息
            int responseCode = con.getResponseCode();
            String responseMessage = con.getResponseMessage();
            InputStream in = con.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();
            con.disconnect();
            
            // 输出响应信息
            System.out.println('Response code: ' + responseCode);
            System.out.println('Response message: ' + responseMessage);
            System.out.println('Response body: ' + response.toString());
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private static byte[] getPostDataBytes(Map<String, String> params) throws UnsupportedEncodingException {
        StringBuilder postData = new StringBuilder();
        for (Map.Entry<String, String> param : params.entrySet()) {
            if (postData.length() != 0) postData.append('&');
            postData.append(URLEncoder.encode(param.getKey(), 'UTF-8'));
            postData.append('=');
            postData.append(URLEncoder.encode(param.getValue(), 'UTF-8'));
        }
        return postData.toString().getBytes('UTF-8');
    }
}

在这个代码中,我们首先创建了一个URL对象,然后通过它创建了一个HttpURLConnection对象。我们将请求方法设置为POST,并将setDoOutput属性设置为true,这样我们就可以向服务器发送请求参数。

接着,我们创建了一个包含请求参数的Map对象,并将其转换为字节数组。我们还设置了请求头的Content-Type和Content-Length属性。

然后,我们通过HttpURLConnection对象的getOutputStream方法获取输出流,并将请求参数发送给服务器。

最后,我们获取响应信息,并使用BufferedReader对象读取响应体。我们将响应体的内容存储在StringBuilder对象中,并在控制台上打印出响应码、响应消息和响应体。

请注意,此示例代码中的URL和请求参数是虚构的,您需要将它们替换为您自己的URL和参数。

Android Studio Java POST 请求实现:字典创建与响应信息获取

原文地址: https://www.cveoy.top/t/topic/m0f9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录