当前位置: 首页 > 知识库问答 >
问题:

如何处理异常执行doInBackground()时发生的错误

佘飞鸣
2023-03-14
try {
    URL url = new URL("http://javalovers.net16.net/showdata.php");
    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.connect();
    switch (connection.getResponseCode()) {
        case HttpURLConnection.HTTP_OK:
            InputStream stream = connection.getInputStream(); //here getting response
            br = new BufferedReader(new InputStreamReader(stream));
            String line = "";
            while ((line = br.readLine()) != null) {
                // buffer.append(line);
                str = str + line;
            }
            break; // fine, go on
        case HttpURLConnection.HTTP_GATEWAY_TIMEOUT:
            break; // retry
        case HttpURLConnection.HTTP_UNAVAILABLE:
            break; // retry, server is unstable
        default:
            break; // abort
    }
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    connection.disconnect();
    try {
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

-致命html" target="_blank">异常:AsyncTask#3进程:kuldeep.mourya.com.smartCollege,pid:10617 java.lang.RuntimeException:在Android.os.AsyncTask$3执行doInBackground()时出错。done(AsyncTask.java:309)在java.util.concurrent.FutureTask.FinishCompletion(futureTask.java:354)在java.util.concurrent.FutureTask.SetException(futureTask.java:354)在rofessor.CollegeNWSFragment$JsonTask.DoinBackground(collegeNWSFragment.java:148)位于android.os.asyncTask$2.call(asynctask.java:295)

有人知道为什么我会出现这个错误吗?

哇!!!我在分离异常时得到了答案,尝试捕获块!

//URL url=new URL("http://javalovers.net16.net/showdata.php");
        URL url = null;// this api link
        try {
            url = new URL("http://vcetsmart.netne.net/showdata.php");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            connection = (HttpURLConnection) url.openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            connection.setRequestMethod("POST");
        } catch (ProtocolException e) {
            e.printStackTrace();
        }
        try {
            connection.connect();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try{
            if(connection.getResponseCode()==200)
            {
                //Toast.makeText(getBaseContext(),"Everything is right",Toast.LENGTH_SHORT).show();
                InputStream stream=connection.getInputStream(); //here getting response
                BufferedReader br = new BufferedReader(new InputStreamReader(stream));
                String line = "";
                while ((line = br.readLine()) != null) {
                    // buffer.append(line);
                    str=str+line;
                }
            }
            else {
                Toast toast= Toast.makeText(getActivity(),"Something goes wrong", Toast.LENGTH_LONG);
                toast.show();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return str;

共有1个答案

南门焱
2023-03-14
br = new BufferedReader(new InputStreamReader(stream));

在上面的代码行中,您在哪里声明并初始化了BufferedReader?

当连接可用时,很有可能您肯定会将数据获取到您的流中。但当您未连接到internet时,流为NULL。

我给您的建议是在您的代码中做一个小改动:

URL url = new URL("http://javalovers.net16.net/showdata.php");
    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.connect();
BufferredReader br = new BufferedReader(new InputStreamReader(stream));
 类似资料: