将参数从android传递给servlet / jsp

时间:2011-01-11 07:31:13

标签: java android jsp

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

下面是我的代码:

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

客户端: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>

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

1 个答案:

答案 0 :(得分:1)

据我所知,这里有两个主要问题:

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

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

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

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