android如何向servlet传数据再接收该servlet传回的数据完整代码示例
时间: 2024-03-25 13:41:15 浏览: 20
以下是一个简单的 Android 应用程序向 Servlet 发送数据并接收响应的完整代码示例:
Android 应用程序:
```java
public class MainActivity extends AppCompatActivity {
private EditText editText;
private TextView textView;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
textView = findViewById(R.id.textView);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String input = editText.getText().toString();
new SendDataTask().execute(input);
}
});
}
private class SendDataTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String input = params[0];
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();
os.write(input.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 input = request.getReader().readLine();
String output = "Received: " + input;
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)