java - 将参数从 android 传递到 servlet/jsp

标签 java android jsp

我无法在我的 jsp 页面中获取从 android 传递的值。我在哪里犯了错误。任何建议!

下面是我的代码:

==========================

客户端:安卓 Activity

package org.test2;

import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class test2Activity extends Activity {
    EditText ed;
    Button ok;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ed=(EditText)findViewById(R.id.EditText01);
        ok=(Button)findViewById(R.id.Button01);
        ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                // Create a new HttpClient and Post Header  
                HttpClient httpclient = new DefaultHttpClient();
                HttpGet request=new HttpGet("http://localhost:8080/t5/jsp1.jsp?name=hiren");
                //HttpPost httppost = new HttpPost("http://localhost:8080/t5/jsp1.jsp?name=hiren"); 
                try {               
                    // Execute HTTP Post Request  
                    HttpResponse response = httpclient.execute(request);                    

                } catch (ClientProtocolException e) {  
                    // TODO Auto-generated catch block  
                }catch (IOException e) {  
                    // TODO Auto-generated catch block  
                }
            }
        });
    }

}

=======================================

服务器:jsp

<%@ page import="java.util.*" language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript"> 
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Parameter value = 
<%=request.getParameter("name") %>
</body>
</html>

=========================================

最佳答案

我认为这里有两个主要问题:

您正在使用 localhost,我想您正在尝试使用 Emulator,所以您必须分配机器的 IP 而不是 localhost,如果假设您有 DHCP,那么在 Android localhost 中应该以这种方式完成 所以尝试

"http://10.0.2.2:8080/myapp/servletname?key=value"

您正在尝试在浏览器中打印,而不是我的建议是尝试在控制台中打印它会显示给您的值,然后您可以将其保存到数据库或执行此过程。

希望这可以解决问题或任何面临此类问题的开发人员。

关于java - 将参数从 android 传递到 servlet/jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4655210/

相关文章:

java - Gson避免反射

java - 如何设计一个也响应后端事件的 Java Swing 模型?

java - 从自定义类中创建数组?

spring-mvc - 在 Spring Boot 应用程序中使用 JSP 和 Thymeleaf View

java - jpanel 背景 魔数(Magic Number)

java - ProGuard:库类的重复定义?

java - 使用 md5 扫描重复文档

java - 带变量的 JSTL 三元语法

java - 在 JSP 中获取多对一 hibernate

java - 通过Tomcat服务器读取意大利字符时出错