android如何向servlet传两个String类型数据再接收该servlet传回的数据
时间: 2024-03-25 20:41:17 浏览: 26
以下是一个 Android 应用程序向 Servlet 发送两个 String 类型数据并接收响应的完整代码示例:
Android 应用程序:
```java
public class MainActivity extends AppCompatActivity {
private EditText editText1, editText2;
private TextView textView;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = findViewById(R.id.editText1);
editText2 = findViewById(R.id.editText2);
textView = findViewById(R.id.textView);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String input1 = editText1.getText().toString();
String input2 = editText2.getText().toString();
new SendDataTask().execute(input1, input2);
}
});
}
private class SendDataTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String input1 = params[0];
String input2 = params[1];
String response = "";
try {
URL url = new URL("http://localhost:8080/myservlet");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
String postData = "input1=" + URLEncoder.encode(input1, "UTF-8") + "&input2=" + URLEncoder.encode(input2, "UTF-8");
os.write(postData.getBytes());
os.flush();
os.close();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
response += line;
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String response) {
textView.setText(response);
}
}
}
```
Servlet 程序:
```java
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String input1 = request.getParameter("input1");
String input2 = request.getParameter("input2");
String output = "Received: " + input1 + ", " + input2;
response.getWriter().write(output);
}
}
```
在本例中,当用户点击按钮时,Android 应用程序将向名为 "myservlet" 的 Servlet 发送一个 POST 请求,并将 EditText 中的两个文本作为请求参数发送。Servlet 接收到请求后,读取请求参数,并将其用作响应的一部分发送回 Android 应用程序。应用程序将响应显示在 TextView 中。
注意:为了使本示例正常工作,您需要将 Android 应用程序和 Servlet 部署到同一个 Web 服务器上,并将 URL 中的 "localhost" 更改为服务器的 IP 地址。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)