在JSP中从Android读取Http post参数

时间:2014-08-13 21:27:11

标签: android jsp

我正在制作一个应用程序,应该向我的jsp发帖请求。我做了所有在例子中写的东西,但它还没有工作。我在服务器上捕获了请求,但所有参数都返回null

Android代码

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");
%>

我做错了什么?

1 个答案:

答案 0 :(得分:1)

试试这个

Android代码

@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>