0

I am authenticating my users using OAuth 2.0, and I have to make an http post request (sending my client id, client secret etc). I think I am doing that successfully. As a response I am supposed to get the access token and other information. But when I print the response in my LogCat it comes out as "org.apache.http.message.BasicHttpResponse@407e7bb0". Where do I find the message? Below is my code and I followed this example http://www.androidhive.info/2011/10/android-making-http-requests/

public class PasteCode extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pastecode);

    final EditText codeinput = (EditText) findViewById(R.id.editText1);

    Button send_btn = (Button) findViewById(R.id.button1);
    send_btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            final String code = codeinput.getText().toString();

            new Thread( new Runnable() {
                @Override
                public void run() {

         HttpClient httpClient = new DefaultHttpClient();
     HttpPost httpPost = new HttpPost("https://accounts.google.com/o/oauth2/auth?");

     ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(5);
     nameValuePair.add(new BasicNameValuePair("code", code));
     nameValuePair.add(new BasicNameValuePair("client_id", "---------"));
     nameValuePair.add(new BasicNameValuePair("client_secret", "---"));
     nameValuePair.add(new BasicNameValuePair("redirect_uri", "urn:ietf:wg..."));
     nameValuePair.add(new BasicNameValuePair("grant_type", "authorization_code"));

try {
    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));

    } catch (UnsupportedEncodingException e) {
    // writing error to Log
     e.printStackTrace();
    }

     // Making HTTP Request

try {
     HttpResponse response = httpClient.execute(httpPost);

     // writing response to log
     Log.d("Http Response:", response.toString());
    } catch (ClientProtocolException e) {
    // writing exception to log
     e.printStackTrace();
     } catch (IOException e) {
     // writing exception to log
     e.printStackTrace();

     }

     }
    }).start();

    }
});
}

}

2 Answers 2

2

You can use BufferedReader:

BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder builder = new StringBuilder();
String str = "";

while ((str = rd.readLine()) != null) {
    builder.append(str);
}

String text = builder.toString();
Sign up to request clarification or add additional context in comments.

Comments

0

The answer from nikis should work fine, but I find this static method found in EntityUtils to be a bit more terse. If you look here, you'll see that the implementations are relatively similar.

1 Comment

Thank you very much both, I got it working using the BufferReader.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.