我正在使用OAuth 2.0对我的用户进行身份验证,我必须发出http post请求(发送我的客户端ID,客户端密码等)。我想我是成功的。作为回应,我应该获取访问令牌和其他信息。但是当我在LogCat中打印响应时,它出现为“org.apache.http.message.BasicHttpResponse@407e7bb0”。我在哪里可以找到该消息?下面是我的代码,我按照这个示例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();
}
});
}
}
答案 0 :(得分:2)
您可以使用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();
答案 1 :(得分:0)
nikis的答案应该可以正常工作,但我觉得这个static method found in EntityUtils要简洁一些。如果你看here,你会发现实现相对类似。