【发布时间】:2012-06-15 15:09:12
【问题描述】:
您好,我正在尝试设置一个简单的连接以通过 android 应用程序下载网页源代码,以便稍后将其与我的 api 结合使用。我的代码下面显示没有错误,但在运行时应用程序在运行此代码时退出。当我注释掉 BufferReader 行时,应用程序会继续正常运行,所以问题一定出在这行代码上:
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
下面是我使用它的完整代码 sn-p。
currentCustomer.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
URL url = null;
url = new URL("http:www.google.com");
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = "";
while((line = reader.readLine()) != null) {
tv.append(line);
}
} catch(Exception e) {
}
}
});
任何帮助将不胜感激:)
这是堆栈跟踪:
06-15 16:15:50.911: D/dalvikvm(925): GC_FOR_ALLOC freed <1K, 4% free 7249K/7495K, paused 59ms
06-15 16:15:51.049: D/dalvikvm(925): GC_CONCURRENT freed <1K, 4% free 7249K/7495K, paused 5ms+4ms
06-15 16:15:51.159: V/TLINE(925): new: android.text.TextLine@40633f50
06-15 16:15:51.489: V/TLINE(925): new: android.text.TextLine@4063be20
06-15 16:15:55.019: W/System.err(925): java.lang.NullPointerException
06-15 16:15:55.039: W/System.err(925): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.hashCode(HttpConnection.java:296)
06-15 16:15:55.039: W/System.err(925): at java.util.HashMap.get(HashMap.java:302)
06-15 16:15:55.039: W/System.err(925): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:73)
06-15 16:15:55.039: W/System.err(925): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:292)
06-15 16:15:55.049: W/System.err(925): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:274)
06-15 16:15:55.049: W/System.err(925): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1038)
06-15 16:15:55.049: W/System.err(925): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:523)
06-15 16:15:55.049: W/System.err(925): at com.kylemcbride.SqueakyClean.SqueakyCleanActivity$1.onClick(SqueakyCleanActivity.java:48)
06-15 16:15:55.049: W/System.err(925): at android.view.View.performClick(View.java:3100)
06-15 16:15:55.049: W/System.err(925): at android.view.View$PerformClick.run(View.java:11644)
06-15 16:15:55.059: W/System.err(925): at android.os.Handler.handleCallback(Handler.java:587)
06-15 16:15:55.059: W/System.err(925): at android.os.Handler.dispatchMessage(Handler.java:92)
06-15 16:15:55.069: W/System.err(925): at android.os.Looper.loop(Looper.java:126)
06-15 16:15:55.069: W/System.err(925): at android.app.ActivityThread.main(ActivityThread.java:3997)
06-15 16:15:55.069: W/System.err(925): at java.lang.reflect.Method.invokeNative(Native Method)
06-15 16:15:55.069: W/System.err(925): at java.lang.reflect.Method.invoke(Method.java:491)
06-15 16:15:55.080: W/System.err(925): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
06-15 16:15:55.080: W/System.err(925): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
06-15 16:15:55.080: W/System.err(925): at dalvik.system.NativeStart.main(Native Method)
【问题讨论】:
标签: android sdk bufferedreader urlconnection