user3364963
user3364963

Reputation: 397

HTTP get and post in android

in my program I have http get which gets data from PHP script. This code is present in async task. This works fine.

But now I want to do HTTP post, where I, the android client, post data to the PHP script, it quires the DB and returns the result of that.

But this is what is confusing me.

  1. Can I get a response from a HTTP post? Or do i need a combination of post and get?

  2. This question I don't expect an answer but if anyone can advise on this would be great. I have one async task which does the HTTP get. Now i want to use the same async to do either HTTP get or post but not both. Is this possible?

Thank you

Here John. A small snippet. My problem is the HTTP StatusLine httpStatus and http entity it does not recognise any of the responses because they are in if statements so the compiler thinks they will not be defined.

            if(params[1] == "GETRESULT")
            {
                HttpGet get = new HttpGet(params[0]);
                HttpResponse r = client.execute(get);
            }

            else //we are posting
            {
                HttpPost post = new HttpPost(params[0]);
                HttpResponse r = client.execute(post);
            }

            StatusLine httpStatus = r.getStatusLine();
            HttpEntity e = r.getEntity();

Upvotes: 0

Views: 105

Answers (2)

chriskvik
chriskvik

Reputation: 1281

Move your HttpResponse r above the if/else statement as HttpResponse someVariable; then you can access it inside your else, and read the result afterwards. You also have to check for NullPointerException, with a try / catch block.

For example like this :

                HttpResponse r;                
                if(params[1] == "GETRESULT")
                {
                    HttpGet get = new HttpGet(params[0]);
                    r = client.execute(get);
                }

                else //we are posting
                {
                    HttpPost post = new HttpPost(params[0]);
                    r = client.execute(post);
                }

                StatusLine httpStatus = r.getStatusLine();
                try {                
                  HttpEntity e = r.getEntity();
                } catch (NullPointerException e) {
                  //Error handling
                }

Upvotes: 0

John Boker
John Boker

Reputation: 83699

  1. You can get a response with post
  2. You can use the same async method as long as you have some logic that changes the request type to POST or GET depending on what you want to do.

some info on HttpPost

// Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
        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
    }

For your code to work you need to declare the Response outside of your if/blocks:

        HttpResponse r = null;

        if(params[1] == "GETRESULT")
        {
            HttpGet get = new HttpGet(params[0]);
            r = client.execute(get);
        }

        else //we are posting
        {
            HttpPost post = new HttpPost(params[0]);
            r = client.execute(post);
        }

        StatusLine httpStatus = r.getStatusLine();
        HttpEntity e = r.getEntity();

Upvotes: 2

Related Questions