【发布时间】:2014-02-18 23:50:08
【问题描述】:
我是 android 新手,我正在尝试发出 http post 请求,但我收到以下错误,我已经发布了我的代码和我的 LogCat 的一些行。我是否应该在不同的课程中执行此操作,如果是,如何执行,或者这只是我必须在当前代码中修复的问题。
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("accounts.google.com");
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:oauth:2.0:oob"));
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();
}
});
}
}
日志视图
02-18 10:03:20.477: W/dalvikvm(3759): threadid=15: 线程未捕获而退出 异常(组=0x4001d760)
02-18 10:03:20.487:E/AndroidRuntime(3759):致命异常:Thread-31
02-18 10:03:20.487: E/AndroidRuntime(3759): java.lang.IllegalStateException: 目标 host 不能为空,也不能在参数中设置。 方案=空,主机=空, path=accounts.google.com
02-18 10:03:20.487: E/AndroidRuntime(3759): at java.lang.Thread.run(Thread.java:1020)
【问题讨论】: