有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java处理internet连接丢失

我设计了一个应用程序,在发出http请求之前检查互联网连接,它工作得很好,但问题是,当有互联网连接时,我切换移动数据,或在搜索数据期间强制失去互联网连接,应用程序崩溃。 如果在获取数据时失去了互联网连接,有没有办法让应用程序报告没有互联网连接?我检查并尝试使用广播接收器,但在我们要声明意图过滤器的安卓清单中,我找不到这些 我还使用加载程序而不是异步任务来执行网络请求 换句话说

我只是需要一个方法来处理 响应验证

该应用程序会检查设备是否已连接到互联网,并做出相应的响应。请求的结果经过验证,以说明服务器响应不好或服务器响应不足,这可能是由于在获取数据期间丢失了internet连接

enter code here   

//这是获取数据的代码

公共课狂热{

public static ArrayList<word> fesh (){


    // Create an empty ArrayList that we can start adding earthquakes to
    ArrayList<word> words = new ArrayList<>();

    // Try to parse the SAMPLE_JSON_RESPONSE. If there's a problem with the way the JSON
    // is formatted, a JSONException exception object will be thrown.
    // Catch the exception so the app doesn't crash, and print the error message to the logs.

    final String RESPONSE = feshjson();
    try {

        // TODO: Parse the response given by the SAMPLE_JSON_RESPONSE string and
        // build up a list of Earthquake objects with the corresponding data.

        JSONObject rootobject = new JSONObject(RESPONSE);

        JSONArray rootarray = rootobject.getJSONArray("features");
        for(int i = 0;i<rootarray.length();i++){

            JSONObject current = rootarray.getJSONObject(i);
            JSONObject properties = current.getJSONObject("properties");
            double magnitude = properties.getDouble("mag");
            String place = properties.getString("place");
            long date = properties.getLong("time");
            String url = properties.getString("url");

            word earthquake = new word(magnitude,place,date,url);
            words.add(earthquake);

        }



    } catch (JSONException e) {
        // If an error is thrown when executing any of the above statements in the "try" block,
        // catch the exception here, so the app doesn't crash. Print a log message
        // with the message from the exception.
        Log.e("QueryUtils", "Problem parsing the earthquake JSON results", e);
    }

    // Return the list of earthquakes
    // getword(words);
    return words;


}




public static String feshjson(){

    HttpURLConnection connection = null;
    InputStream stream = null;
    BufferedReader reader = null;

    try {
        URL url = new URL("https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2012-01-01&endtime=2012-12-01&minmagnitude=6");
        connection = (HttpURLConnection) url.openConnection();
        connection.connect();
        stream = connection.getInputStream();
        reader = new BufferedReader(new InputStreamReader(stream));
        String line = "";
        StringBuffer buffer = new StringBuffer();
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }
        return  buffer.toString();
    } catch (MalformedURLException e) {
        e.printStackTrace();
        new MainActivity().Nonetwork();
    }
    catch (ConnectException ex)
    {
        ex.printStackTrace();
         new MainActivity().Nonetwork();
    }

    catch (SocketException ex)
    {
        ex.printStackTrace();
        new MainActivity().Nonetwork();
    }
    catch (SocketTimeoutException ex)
    {
        ex.printStackTrace();
        new MainActivity().Nonetwork();
    }
    catch(NullPointerException ex)
    {
        ex.printStackTrace();
        new MainActivity().Nonetwork();
    }
    catch (Exception e)
    {

        e.printStackTrace();

        new MainActivity().Nonetwork();
    }

     finally {
        if (connection != null) {
            connection.disconnect();
        }
        try {
            if (stream != null) {
                stream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            new MainActivity().Nonetwork();
        }
    }
    return null;



}

}

//this is the method to display no network 
public void Nonetwork(){
TextView text = (TextView) findViewById(R.id.texto);
text.setText("no internet");
text.setVisibility(View.VISIBLE);

}``

//this is what my Log looks like
05-25 23:50:46.029 22283-22308/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                               Process: com.example.安卓.myquake, PID: 22283
                                               java.lang.RuntimeException: An error occured while executing doInBackground()
                                                   at 安卓.os.AsyncTask$3.done(AsyncTask.java:300)
                                                   at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
                                                   at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
                                                   at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                   at java.lang.Thread.run(Thread.java:818)
                                                Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
                                                   at 安卓.os.Handler.<init>(Handler.java:200)
                                                   at 安卓.os.Handler.<init>(Handler.java:114)
                                                   at 安卓.app.Activity.<init>(Activity.java:793)
                                                   at 安卓.support.v4.app.BaseFragmentActivityGingerbread.<init>(BaseFragmentActivityGingerbread.java:34)
                                                   at 安卓.support.v4.app.BaseFragmentActivityHoneycomb.<init>(BaseFragmentActivityHoneycomb.java:29)
                                                   at 安卓.support.v4.app.BaseFragmentActivityJB.<init>(BaseFragmentActivityJB.java:29)
                                                   at 安卓.support.v4.app.FragmentActivity.<init>(FragmentActivity.java:77)
                                                   at 安卓.support.v7.app.AppCompatActivity.<init>(AppCompatActivity.java:64)
                                                   at com.example.安卓.myquake.MainActivity.<init>(MainActivity.java:0)
                                                   at com.example.安卓.myquake.Fesh.feshjson(Fesh.java:129)
                                                   at com.example.安卓.myquake.Fesh.fesh(Fesh.java:39)
                                                   at com.example.安卓.myquake.Arrayloader.loadInBackground(Arrayloader.java:28)
                                                   at com.example.安卓.myquake.Arrayloader.loadInBackground(Arrayloader.java:18)
                                                   at 安卓.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:312)
                                                   at 安卓.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
                                                   at 安卓.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:57)
                                                   at 安卓.os.AsyncTask$2.call(AsyncTask.java:288)
                                                   at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                                   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                                                   at java.lang.Thread.run(Thread.java:818) 

共 (0) 个答案