从android传递参数到servlet/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>
======================= =================
i m not able to get the value that is pass from android in my jsp page. where i made make mistake. Any suggestion!
below is my code:
==========================
Client : 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
}
}
});
}
}
=======================================
server : 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>
=========================================
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据我的说法,这里有两个主要问题:
你正在使用 localhost,我想你正在尝试使用模拟器,所以你必须分配你的机器的 IP,而不是 localhost,如果假设你有 DHCP,那么在 Android localhost 中应该以这种方式完成所以尝试
"http://10.0.2.2:8080/myapp/servletname?key=value"
您正在尝试在浏览器中打印,而不是我的建议是尝试在控制台中打印该值,它会向您显示并且然后你可以将其保存到数据库或执行该过程。
希望这可以解决问题或任何面临此类问题的开发人员。
Two major problems here according to me:
You are using localhost and I suppose you are trying to use Emulator so instead of localhost you have to assign IP of your machine and if suppose you have DHCP then in Android localhost should be done in this way So try
"http://10.0.2.2:8080/myapp/servletname?key=value"
You are trying to print in browser instead my suggestion is try to print the value in Console it will show you and then you can save it to DB or do the process.
Hope this may solve the problem or any developer facing such a problem.