gpt4 book ai didi

android - inputStream.read() 空指针异常

转载 作者:行者123 更新时间:2023-11-30 04:22:33 25 4
gpt4 key购买 nike

我有时会出错,但不明白为什么:

代码:

public void newConnect()
{ try {
urlka = new URL(myUrl);

} catch (MalformedURLException e) {

e.printStackTrace();
}
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) urlka.openConnection();
conn.connect();
Log.v("Responcse code","Response code " +conn.getResponseCode());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
try {
in = new BufferedInputStream(conn.getInputStream());

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
result = readStream(in); /// line 1198

}
finally {
conn.disconnect();
}

读取数据流()

private String readStream(InputStream is)
{
try
{
ByteArrayOutputStream bo = new ByteArrayOutputStream();
int i = is.read(); /// **error was here** line 1247


while(i != -1)
{
bo.write(i);

i = is.read();
}

return bo.toString();
} catch (IOException e)
{
return ""+e;
}
}
}

日志:

java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:200)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
at java.lang.Thread.run(Thread.java:1096)
Caused by: java.lang.NullPointerException
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl$LocalCloseInputStream.read(HttpURLConnectionImpl.java:167)
at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:157)
at java.io.BufferedInputStream.read(BufferedInputStream.java:243)
at myApp.activity.com.myApp.readStream(myApp.java:1247)
at myApp.activity.com.myApp.newConnect(myApp.java:1198)
at myApp.activity.com.myApp$newRun.doInBackground(myApp.java:424)
at myApp.activity.com.myApp$newRun.doInBackground(myApp.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

有时会出现这种错误。但它是错误的:(有什么能帮我解决这个问题吗?我无法理解为什么会出现此错误。

问候,彼得。

最佳答案

有时可能会出现连接问题,因为 in = new BufferedInputStream(conn.getInputStream());抛出错误,并且输入流保持为空。您还需要处理 null 大小写以解决此问题。

关于android - inputStream.read() 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9043942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com