1
HttpPost httpPost = new HttpPost("MyWebsiteURL");
        try {
            httpPost.setEntity(new StringEntity("https://www.googleapis.com/plus/v1/people/me?key="+token));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            InputStream is = httpEntity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            String json = sb.toString();
            Log.i("JSON", json);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40
ashokgujju
  • 320
  • 2
  • 4
  • 17
  • i think we can not pass parameters in URL using post method.. – Pragnesh Ghoda シ Jul 31 '14 at 06:23
  • @Prag's then how can I establish connection with mywebsite using google token? – ashokgujju Jul 31 '14 at 06:24
  • @Prag's sorry.. URL is like Mywebsite/auth/auth2_login – ashokgujju Jul 31 '14 at 06:27
  • use BasicNameValuePair for passing token – Pragnesh Ghoda シ Jul 31 '14 at 06:33
  • @Prag's this is the syntax for BasicNameValuePair --- its got two parameters how can I pass just token. List pairs = new ArrayList(); pairs.add(new BasicNameValuePair("key1", "value1")); post.setEntity(new UrlEncodedFormEntity(pairs)); – ashokgujju Jul 31 '14 at 06:39
  • @Prag'sシ this is the error I'm receiving. Could you explain me about this. ' 502 Proxy Error

    Proxy Error

    The proxy server received an invalid response from an upstream server.
    The proxy server could not handle the request POST /login.

    Reason: Error reading from remote server


    Apache/2.2.15 (Red Hat) Server at todoed-preseed.rhcloud.com Port 443
    '
    – ashokgujju Aug 02 '14 at 14:18
  • it means your code is correct and you are receiving output from your server side....but you are passing wrong credentials to it – Pragnesh Ghoda シ Aug 03 '14 at 07:41

3 Answers3

1

First you cannot pass parameter like this in http post, use below code for your use, there might be some compilation error as am not using and IDE for checking code which am posting, main point is to show you how to pass post parameters using http post, which is by using NameValuePairs, please adjust your url accordingly

        try {
           HttpPost httppost = new HttpPost("https://www.googleapis.com/plus/v1/people/me");
           List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
           nameValuePairs.add(new BasicNameValuePair("key", "12345"));
           httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            InputStream is = httpEntity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            String json = sb.toString();
            Log.i("JSON", json);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
Techfist
  • 4,314
  • 6
  • 22
  • 32
  • HttpPost parameter = String representing your POST's destination. I want to make HttpPost request to my website. – ashokgujju Jul 31 '14 at 06:34
  • new HttpPost("https://www.googleapis.com/plus/v1/people/me"), pass whatever website you want to refer, while creating new HttpPost object. – Techfist Jul 31 '14 at 06:36
  • I have problem with new HttpPost().setEntity() parameter. not with HttpPost object. I want to post this ("https://www.googleapis.com/plus/v1/people/me?key="+token) to my website. – ashokgujju Jul 31 '14 at 06:46
  • Thats am trying to help you with, key is a parameter for you, and i just mentioned to you the way to post it !! – Techfist Jul 31 '14 at 08:53
0

There is a Difference between POST and GET method...

In GET METHOD you can pass data with URL....
but in POST method you can not pass data with URL... you have to pass it as entity......... Do the following for Passing Data To the URL

Try This..

      DefaultHttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
        HttpPost httpPost = new HttpPost(WEBSITE_URL);
        try{    
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

            //use this to pass variables while using POST method
            // add an HTTP variable and value pair
            nameValuePairs.add(new BasicNameValuePair("key name","key value")); 
            nameValuePairs.add(new BasicNameValuePair("key name","key value"));
            nameValuePairs.add(new BasicNameValuePair("key name","key value"));

            // passing data to the URL
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            //do the following..

           }

For your solution You should pass

nameValuePairs.add(new BasicNameValuePair("key",token));

Where,

 Key - variable or key you defined in Your page (Backend side)     
 token = is a value which you want to pass..........
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40
0

This is the posting for authentication. This will return a HttpResponse containing the access token in which you will use when you do a HttpGet request.

  try {
        Log.i(tag, "Starting doHTTPPost");
        List<NameValuePair> pairs = new ArrayList<NameValuePair>();
        pairs.add(new BasicNameValuePair("KEY", "VALUE"));

        /* EXAMPLE of pairs */
        pairs.add(new BasicNameValuePair("client_id","theGreatSecret12345"));
        pairs.add(new BasicNameValuePair("username", "purple"));
        pairs.add(new BasicNameValuePair("password", "asjdf098732hkndfa"));

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("The API Server you want to access");
        post.setEntity(new UrlEncodedFormEntity(pairs));
        HttpResponse response = client.execute(post);
        HttpResponse apiResponse = response;
        String resultFromServerAsAString = EntityUtils.toString(response.getEntity());

        Log.i(tag, "Response statusCode : "+response.getStatusLine().getStatusCode());
        Log.i(tag, "Response StatusLine : "+response.getStatusLine());
        Log.i(tag, "Ending doHTTPPost");

    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
gorbos
  • 311
  • 1
  • 7