使用httpclient无法POST表单数据

时间:2015-10-27 07:46:23

标签: java android forms post

我有一个网页,它接受一个卷号,然后在同一页面输出结果。网站页面来源是这样的:

 <FORM name="new" ACTION=vitavi.php METHOD=POST >
 <P>Enter the University Seat No: <INPUT TYPE=TEXT NAME="rid" SIZE=20 MAXLENGTH=50><BR><BR><BR><BR>
 <INPUT TYPE=SUBMIT NAME="submit" VALUE="SUBMIT" ALIGN = "center">

我尝试使用post方法获取结果。

这是我的代码

 public String makeServiceCall(String url, int method,
        List<NameValuePair> params) {
    Log.e("Called", "New Called");
    try {
        // http client
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;

        // Checking http request method type
        if (method == POST) {
            HttpPost httpPost = new HttpPost(url);
            // adding post params
            if (params != null) {
                httpPost.setEntity(new UrlEncodedFormEntity(params));
                Log.e("Called", "This called");
            }

            httpResponse = httpClient.execute(httpPost);


        } else if (method == GET) {
            // appending params to url
            if (params != null) {
                String paramString = URLEncodedUtils
                        .format(params, "utf-8");
                url += "?" + paramString;
            }
            HttpGet httpGet = new HttpGet(url); 
            httpResponse = httpClient.execute(httpGet); 
        }
        httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity);
       // Log.e("Hiiii",response);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

我在MainActivity中添加了这样的参数:

  final Httpcall res=new Httpcall();
                    final List<NameValuePair> param=new ArrayList<NameValuePair>();
                    param.add(new BasicNameValuePair("rid", r));
                    new Thread(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            HtmlStr=res.makeServiceCall("http://results.vtu.ac.in/vitavi.php",Httpcall.POST, param);

但是每次第一页的页面,即接受滚动的页面都显示为bieng。我哪里错了?

提前致谢。

1 个答案:

答案 0 :(得分:0)

我现在解决了这个问题。发布这个答案,以便任何面临同样问题的人都不必像我一样等待:

我不知道我们还必须为Submit Button添加一个BasicNameValuePair。

<INPUT TYPE=SUBMIT NAME="submit" VALUE="SUBMIT" ALIGN = "center">

因此,在添加了卷号后,我添加了另一对:

param.add(new BasicNameValuePair("rid", r));
param.add(new BasicNameValuePair("submit", "SUBMIT"));