У меня иногда возникает ошибка, но я не понимаю, почему:
Код:
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();
}
readStrem ()
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;
}
}
}
Logcat:
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)
Эта ошибка иногда бывает.Но это ошибка :( Может ли кто-нибудь помочь мне решить эту проблему? Я не могу понять, почему эта ошибка.
С уважением, Питер.