1

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;
        }
Community
  • 1
  • 1

2 Answers2

0

Using 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
        }
});
Lalith Mohan
  • 3,836
  • 5
  • 21
  • 36
0

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.
Ritesh Gune
  • 16,629
  • 6
  • 44
  • 72