最近在弄人人网的api。在调用的过程中发现Util下的openUrl()的函数老是会包IOException错误。还没有详细的错误信息。初步判断是conn.connect()方法执行的时候发生的错误
代码:public static String openUrl(String url, String method, Bundle params) {
if (method.equals("GET")) {
url = url + "?" + encodeUrl(params);
}
String response = ""; try {
URL urll = new URL(url);
URLConnection rulConnection = urll.openConnection();
HttpURLConnection conn = (HttpURLConnection) rulConnection;
conn.setRequestProperty("User-Agent", USER_AGENT_SDK);
if (method.equals("POST")) {
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
conn.setInstanceFollowRedirects(true);
try {
conn.connect(); ///①
} catch (Exception e) {
e.printStackTrace();
}
// try {
// conn.getOutputStream().write(encodeUrl(params).getBytes("UTF-8"));
// } catch (Exception e) {
// e.printStackTrace();
//}
}
InputStream is = null;
int responseCode = conn.getResponseCode();
if (responseCode == 200) {
is = conn.getInputStream();
} else {
is = conn.getErrorStream();
}
response = read(is);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
} return response;
}在①处会抛出IOException。只有:java.io.IOException。没有详细的错误信息。菜鸟我很是不解。想请问各位大神,在connect之前要对哪些http头进行设置。对于那个url是人人网提供的,我才应该不会错。
求大神帮忙解决啊。。Android
代码:public static String openUrl(String url, String method, Bundle params) {
if (method.equals("GET")) {
url = url + "?" + encodeUrl(params);
}
String response = ""; try {
URL urll = new URL(url);
URLConnection rulConnection = urll.openConnection();
HttpURLConnection conn = (HttpURLConnection) rulConnection;
conn.setRequestProperty("User-Agent", USER_AGENT_SDK);
if (method.equals("POST")) {
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
conn.setInstanceFollowRedirects(true);
try {
conn.connect(); ///①
} catch (Exception e) {
e.printStackTrace();
}
// try {
// conn.getOutputStream().write(encodeUrl(params).getBytes("UTF-8"));
// } catch (Exception e) {
// e.printStackTrace();
//}
}
InputStream is = null;
int responseCode = conn.getResponseCode();
if (responseCode == 200) {
is = conn.getInputStream();
} else {
is = conn.getErrorStream();
}
response = read(is);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
} return response;
}在①处会抛出IOException。只有:java.io.IOException。没有详细的错误信息。菜鸟我很是不解。想请问各位大神,在connect之前要对哪些http头进行设置。对于那个url是人人网提供的,我才应该不会错。
求大神帮忙解决啊。。Android
傻× 了。4.0系统后在主线程里面不可以进行网络连接等。把处理放到Runnable里面就可以了。可是为什么之前报的错误是IOException呢?菜鸟不懂。。
public void run() {
Looper.prepare();
renren.authorize(
BindAccountActivity.this, null,
listener, 0);
Looper.loop();
}
}).start();还是会有点问题,登录时弹出的webwiew会循环打印出一段警告日志05-10 18:50:02.215: W/webview(26084): java.lang.Throwable: Warning: A WebView method was called on thread 'Thread-1331'. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads.
05-10 18:50:02.215: W/webview(26084): at android.webkit.WebView.checkThread(WebView.java:13888)
05-10 18:50:02.215: W/webview(26084): at android.webkit.WebView.getSettings(WebView.java:5337)
05-10 18:50:02.215: W/webview(26084): at android.webkit.WebView$10.run(WebView.java:5380)
05-10 18:50:02.215: W/webview(26084): at android.os.Handler.handleCallback(Handler.java:605)
05-10 18:50:02.215: W/webview(26084): at android.os.Handler.dispatchMessage(Handler.java:92)
05-10 18:50:02.215: W/webview(26084): at android.os.Looper.loop(Looper.java:137)
05-10 18:50:02.215: W/webview(26084): at com.hoperun.gymboree.more.BindAccountActivity$2$1.run(BindAccountActivity.java:83)
05-10 18:50:02.215: W/webview(26084): at java.lang.Thread.run(Thread.java:856)
05-10 18:50:02.220: W/webview(26084): java.lang.Throwable: Warning: A WebView method was called on thread 'Thread-1331'. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads.
05-10 18:50:02.220: W/webview(26084): at android.webkit.WebView.checkThread(WebView.java:13888)
05-10 18:50:02.220: W/webview(26084): at android.webkit.WebView.getSettings(WebView.java:5337)
05-10 18:50:02.220: W/webview(26084): at android.webkit.WebView$10.run(WebView.java:5380)
05-10 18:50:02.220: W/webview(26084): at android.os.Handler.handleCallback(Handler.java:605)
05-10 18:50:02.220: W/webview(26084): at android.os.Handler.dispatchMessage(Handler.java:92)
05-10 18:50:02.220: W/webview(26084): at android.os.Looper.loop(Looper.java:137)
05-10 18:50:02.220: W/webview(26084): at com.hoperun.gymboree.more.BindAccountActivity$2$1.run(BindAccountActivity.java:83)
05-10 18:50:02.220: W/webview(26084): at java.lang.Thread.run(Thread.java:856)
05-10 18:50:02.220: W/webview(26084): java.lang.Throwable: Warning: A WebView method was called on thread 'Thread-1331'. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads.
05-10 18:50:02.220: W/webview(26084): at android.webkit.WebView.checkThread(WebView.java:13888)
05-10 18:50:02.220: W/webview(26084): at android.webkit.WebView.destroy(WebView.java:2473)
05-10 18:50:02.220: W/webview(26084): at android.webkit.WebView$10.run(WebView.java:5383)
05-10 18:50:02.220: W/webview(26084): at android.os.Handler.handleCallback(Handler.java:605)
05-10 18:50:02.220: W/webview(26084): at android.os.Handler.dispatchMessage(Handler.java:92)
05-10 18:50:02.220: W/webview(26084): at android.os.Looper.loop(Looper.java:137)
05-10 18:50:02.220: W/webview(26084): at com.hoperun.gymboree.more.BindAccountActivity$2$1.run(BindAccountActivity.java:83)
05-10 18:50:02.220: W/webview(26084): at java.lang.Thread.run(Thread.java:856)
另外Util.java文件中的read方法,在2.2以上的系统中都会报错,BufferedInputStream is closed,据说这是android2.3及以上的版本的一个 bug,蛋疼