如何在Android应用中使用HttpPost在webform上发布一个值

时间:2013-09-10 10:56:32

标签: android jsoup

这里首先要对网址发出http请求,然后想要将一些数据发布到用户输入的网址。我正在使用JSOUP。任何人都可以建议一些简单的方法。感觉很困难Ktutorial.followed this任何人建议任何示例或简单教程。谢谢提前

在此处添加代码

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;
        }

2 个答案:

答案 0 :(得分:0)

使用AsyncHttpClient

AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();

params.put("username",username);
params.put("password",pass);

client.post("http://www.example.com/", params, new AsyncHttpResponseHandler() {
    @Override
        public void onSuccess(String response) {
            //parse data with Jsoup
        }
});

答案 1 :(得分:0)

使用JSOUP,您可以执行Post,如下所示:

// 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.