学会了安卓连接服务端的方法,使用OKhttp3
服务端代码:
package com.example.okhttp3; import java.io.IOException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.fasterxml.jackson.databind.ObjectMapper; @WebServlet("/api/hello") public class HelloServlet extends HttpServlet { private final ObjectMapper objectMapper = new ObjectMapper(); @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { try { System.out.println("已连接"); // 获取客户端的基本信息 String clientAddress = request.getRemoteAddr(); String requestMethod = request.getMethod(); String requestURI = request.getRequestURI(); // 打印访问信息到控制台 System.out.println("Client Address: " + clientAddress); System.out.println("Request Method: " + requestMethod); System.out.println("Request URI: " + requestURI); String useSSL = request.getParameter("useSSL"); String message; if (useSSL != null && useSSL.equals("true")) { message = "Hello, this is an API endpoint with SSL enabled!"; } else { message = "Hello, this is an API endpoint with SSL disabled!"; } // 构造一个包含 message 的 JSON 响应 String jsonResponse = objectMapper.writeValueAsString(new ApiResponse(message)); response.setContentType("application/json"); response.getWriter().println(jsonResponse); } catch (Exception e) { e.printStackTrace(); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } } // 响应数据的 POJO 类 private static class ApiResponse { private final String message; public ApiResponse(String message) { this.message = message; } public String getMessage() { return message; } } }
安卓端代码:
package com.example.okhttp3; import android.annotation.SuppressLint; import android.os.Bundle; import android.util.Log; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import com.google.gson.Gson; import java.io.IOException; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class MainActivity extends AppCompatActivity { private OkHttpClient client = new OkHttpClient(); private TextView textView; @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 关联布局文件中的 TextView textView = findViewById(R.id.textView); // 创建一个请求 Request request = new Request.Builder() .url("http://192.168.227.177:8080/api/hello?useSSL=true") .build(); // 发起异步请求 client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.d("连接", "连接失败"); // 处理连接失败的情况 e.printStackTrace(); } @Override public void onResponse(Call call, Response response) throws IOException { Log.d("连接", "连接成功"); // 处理连接成功的情况 if (response.isSuccessful()) { // 从响应中获取数据 String responseData = response.body().string(); // 解析JSON数据并更新UI updateUIWithResponse(responseData); } else { // 处理响应失败的情况 updateUIWithError(); } } }); } // 更新UI显示响应数据 private void updateUIWithResponse(final String responseData) { runOnUiThread(new Runnable() { @Override public void run() { // 使用 Gson 解析 JSON 数据为 ApiResponse 对象 Gson gson = new Gson(); ApiResponse apiResponse = gson.fromJson(responseData, ApiResponse.class); // 设置 TextView 显示解析后的消息 textView.setText(apiResponse.getMessage()); } }); } // 更新UI显示连接失败消息 private void updateUIWithError() { runOnUiThread(new Runnable() { @Override public void run() { // 设置 TextView 显示连接失败消息 textView.setText("连接失败"); } }); } }
标签:String,void,第五,安卓端,new,import,message,public,服务端 From: https://www.cnblogs.com/atrue/p/18095254