gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:46:59 24 4
gpt4 key购买 nike

我无法在我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com