android - 在 JSP 中从 Android 读取 Http post 参数

标签 android jsp

我正在制作一个应用程序,它应该向我的 jsp 发出发布请求。我做了示例中写的所有内容,但它仍然无法正常工作。我在服务器上捕获了请求,但所有参数都返回 null

安卓代码

HttpClient httpClient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
HttpPost httpPost = new HttpPost(uri);

try {
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("group", group));
    nameValuePairs.add(new BasicNameValuePair("increment", String.valueOf(additionalPoints)));
    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    response = httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
} catch (IOException e) {
}

JSP代码

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%
    String group = request.getParameter("group");
    String incrementStr = request.getParameter("increment");
%>

我做错了什么?

最佳答案

这样试试

安卓代码

@Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        try
        {
            setContentView(R.layout.main);
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(url);

            List<NameValuePair> pairs = new ArrayList<NameValuePair>();
            paris.add(new BasicNameValuePair("group", group));
            paris.add(new BasicNameValuePair("increment", String.valueOf(additionalPoints)));
            post.setEntity(new UrlEncodedFormEntity(pairs));
            HttpResponse response = client.execute(post);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

JSP代码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.io.*" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<%
    String group=request.getParameter("group");
    String increment=request.getParameter("increment");
%>

</html>

关于android - 在 JSP 中从 Android 读取 Http post 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25296448/

相关文章:

android - 无法使用上述方法连接打印机 Zebra MZ 220

java - JSP 在 tomcat 上找不到类错误 org.hibernate.Session

java - tomcat中jsp项目文件夹放在哪里托管?

c# - Xamarin Android httpwebrequest,无法访问已处置的对象

android - 服务绑定(bind)和两个 Activity 奇怪的情况

android - 字节分配内存不足(位图作为字符串到使用 soap 的 web 服务)

javascript - 如何在单击按钮时添加文本框?

java - JSP 页面上未显示区域字符

java - <c :set var ="s" value ="class=\"selected\"" scope ="request"/> sets unexpected String in jSTL

java - Android库模块中的依赖