2

I'm trying to increase my knowledge to Android and trying to code a small app for my personal needs. I'm trying to post data via the HTTP Post method on a test server. The request is sent ok, but now, I'm trying to display the response, which is an HTML page with the dump of my request. Here is an extract of my code, it is basically a few EditText fields, and button that sends the request. The following code is the listener for that button.

        validateButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://posttestserver.com/post.php?dump&html&dir=mydir&status_code=200");

            try {
                // Gathering data
                String value01 = nb01Spinner.getSelectedItem().toString();
                String value02 = nb02EditText.getText().toString();
                String value03 = nb03EditText.getText().toString();
                String value04 = nb04EditText.getText().toString();

                // Add data to value pairs
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(04);
                nameValuePairs.add(new BasicNameValuePair("test01", value01));
                nameValuePairs.add(new BasicNameValuePair("test02", value02)); // 
                nameValuePairs.add(new BasicNameValuePair("test03", value03));
                nameValuePairs.add(new BasicNameValuePair("test04", value04));

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }

I'm not sure if I need to create another Activity or not... I suppose I also have to create a webview aswell, but I'm a bit lost. For now the "raw" HTML would be fine, but afterwards I will need to parse the data, and extract only the strings I need.

So I would need help (an a good and simple example !)

Thank you.

4 Answers4

2
String ret = EntityUtils.toString(response.getEntity());

Maybe this will help?

davidcesarino
  • 15,491
  • 15
  • 66
  • 108
Klaasvaak
  • 5,430
  • 12
  • 43
  • 67
0

Very simple approach is Take textview the way you have taken button widget. and what ever response you got set in the textview. you will be able to see the response. else use the Log to log your response in the logcat.

Dinesh Prajapati
  • 8,719
  • 5
  • 27
  • 47
0

I am using:

Log.d("log_response", response.getStatusLine().toString());
davidcesarino
  • 15,491
  • 15
  • 66
  • 108
Pankaj Parag
  • 317
  • 1
  • 3
  • 17
0

This is how you get the Http response :

            byte[] buffer = new byte[1024];
            httpclient = new DefaultHttpClient();
            httppost = new HttpPost("http://www.rpc.booom.com");



            postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("params","1"));
            //.......
            httppost.setEntity(new UrlEncodedFormEntity(postParameters));

            HttpResponse response = httpclient.execute(httppost);
            Log.w("Response ","Status line : "+ response.getStatusLine().toString());
            buffer = EntityUtils.toString(response.getEntity()).getBytes();
Android-Droid
  • 13,649
  • 37
  • 107
  • 184