如何在Android应用程序使用HttpPost一个WebForm张贴价值 [英] How post a value on a webform using HttpPost in android app

查看:179
本文介绍了如何在Android应用程序使用HttpPost一个WebForm张贴价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面首先要进行HTTP请求到URL,然后想发布一些数据网址我使用 JSOUP 这是由user.here输入。可以任何一个建议一些简单的way.Felt难以Ktutorial.followed 的任何那么建议任何例子或者容易tutorial.Thanks提前

下面添加code

 字符串strResp = NULL;
            HttpClient的客户端=新DefaultHttpClient();
            HttpPost后=新HttpPost(
                    http://motinfo.direct.gov.uk/internet/jsp/ECHID-Internet-History-Request.jsp);            清单<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;(2);
            nameValuePairs.add(新BasicNameValuePair(
                    从号牌车辆登记号码,DU06BFZ));
            namevaluepairs中
                    。新增(新BasicNameValuePair(
                            MOT测试人数从-VT20测试证书,VT30拒绝证书,
                            435294573022));            / *
             * nameValuePairs.add(新BasicNameValuePair(MOT测试号,
             *000000));
             * /            尝试{
                loadingProgressBar =(进度)findViewById(R.id.progressBar1);
                loadingProgressBar.setVisibility(View.VISIBLE);
                post.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
                HTT presponse响应= client.execute(岗位);
                strResp = EntityUtils.toString(response.getEntity());
                文献文件= Jsoup.parse(strResp);
                元件连结= document.select(A)的第一();
                。字符串文本= document.body的()文本();
                }赶上(UnsupportedEncodingException五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(ClientProtocolException E){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            返回strResp;
        }


解决方案

使用 JSOUP ,你可以做发表作为如下:

  //需要http协议
  文档的DOC = Jsoup.connect(http://www.something.com)// YOUR_URL
  。数据(key_one,value_one)
  。数据(key_two,value_two)
  //并在POST请求传递等隐藏字段。
  //我们推荐指定Jsoup一个的userAgent,以避免HTTP 403错误消息。
  .userAgent(Mozilla的)
  。员额();
   的System.out.println(DOC); //将打印页面的HTML源,你要连接。

Here first want to make http request to url and then want to post some data to url which is entered by user.here i am using JSOUP. Could any one suggest some easy way.Felt difficult with Ktutorial.followed this any one suggest any example or easy tutorial.Thanks in advance

adding code here

String strResp = null;
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(
                    "http://motinfo.direct.gov.uk/internet/jsp/ECHID-Internet-History-Request.jsp");

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair(
                    "Vehicle registration mark from number plate", "DU06BFZ"));
            nameValuePairs
                    .add(new BasicNameValuePair(
                            " MOT test number from-VT20 test certificate-VT30 refusal certificate ",
                            "435294573022"));

            /*
             * nameValuePairs.add(new BasicNameValuePair("MOT test number",
             * "000000"));
             */

            try {
                loadingProgressBar = (ProgressBar) findViewById(R.id.progressBar1);
                loadingProgressBar.setVisibility(View.VISIBLE);
                post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = client.execute(post);
                strResp = EntityUtils.toString(response.getEntity());
                Document document = Jsoup.parse(strResp);
                Element link = document.select("a").first();
                String text = document.body().text();       
                } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return strResp;
        }

解决方案

Using JSOUP you can do Post as follows:

// need http protocol
  Document doc = Jsoup.connect("http://www.something.com") // your_url
  .data("key_one", "value_one")
  .data("key_two", "value_two")
  // and other hidden fields which are being passed in post request.
  //It’s recommended to specify a "userAgent" in Jsoup, to avoid HTTP 403 error messages.
  .userAgent("Mozilla")  
  .post();
   System.out.println(doc); // will print html source of page ,you are trying to connect.

这篇关于如何在Android应用程序使用HttpPost一个WebForm张贴价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆