java Android HTTP异常
我正在使用以下代码登录Dropbox,但它会引发异常
代码:
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("https://www.dropbox.com/login");
// set values you'd like to send
List pairs = new ArrayList();
pairs.add(new BasicNameValuePair("login_email", "my email id"));
pairs.add(new BasicNameValuePair("login_password", "******"));
/*'login_email' and 'login_password' i found html form fields name by viewing the page source of https://www.dropbox.com/login */
try {
post.setEntity(new UrlEncodedFormEntity(pairs));
// set ResponseHandler to handle String responses
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = client.execute(post, responseHandler);
Log.v("HttpPost", "Response: " + response);
if (response.contains("SUCCESS")){
// express your joy here!
} else {
// pop a sad Toast message here...
}
} catch (Exception e) {
Log.e("", String.valueOf(e));
}
}
Logcat
11-14 23:17:35.734: E/Trace(900): error opening trace file: No such file or directory (2)
11-14 23:17:38.793: D/dalvikvm(900): GC_CONCURRENT freed 251K, 4% free 8194K/8519K, paused 126ms+9ms, total 234ms
11-14 23:17:39.895: E/(900): org.apache.http.client.HttpResponseException: Forbidden
11-14 23:17:40.364: I/Choreographer(900): Skipped 75 frames! The application may be doing too much work on its main thread.
11-14 23:17:40.394: D/gralloc_goldfish(900): Emulator without GPU emulation detected.
11-14 23:17:40.784: I/Choreographer(900): Skipped 62 frames! The application may be doing too much work on its main thread.
11-14 23:17:43.614: W/chromium(900): external/chromium/net/disk_cache/backend_impl.cc:1829: [1114/231743:WARNING:backend_impl.cc(1829)] Destroying invalid entry.
# 1 楼答案
通常建议使用AsyncTask在单独的线程而不是主线程上进行网络活动
下面是关于Connecting to the Network的android教程