【发布时间】:2017-05-27 21:33:45
【问题描述】:
我设计了一个应用程序,在发出 http 请求之前检查互联网连接,它工作正常,但问题是,当有互联网连接并且我在此期间切换移动数据或强制断开互联网连接时,它会搜索数据,应用程序崩溃。 如果在获取数据时互联网连接丢失,有没有办法让应用程序报告没有互联网连接,我检查并尝试使用广播接收器,但在我们要声明意图过滤器的 android 清单中,我找不到这些 我也在使用加载器而不是异步任务来执行网络请求 换句话说
我只需要一种方法来处理 响应验证
应用程序会检查设备是否已连接到互联网并做出适当响应。验证请求的结果以说明服务器响应错误或缺少服务器响应,这可能是由于在获取数据期间失去互联网连接
enter code here
//这是获取数据的代码
公共类 Fesh {
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.android.myquake, PID: 22283
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.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 android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at android.app.Activity.<init>(Activity.java:793)
at android.support.v4.app.BaseFragmentActivityGingerbread.<init>(BaseFragmentActivityGingerbread.java:34)
at android.support.v4.app.BaseFragmentActivityHoneycomb.<init>(BaseFragmentActivityHoneycomb.java:29)
at android.support.v4.app.BaseFragmentActivityJB.<init>(BaseFragmentActivityJB.java:29)
at android.support.v4.app.FragmentActivity.<init>(FragmentActivity.java:77)
at android.support.v7.app.AppCompatActivity.<init>(AppCompatActivity.java:64)
at com.example.android.myquake.MainActivity.<init>(MainActivity.java:0)
at com.example.android.myquake.Fesh.feshjson(Fesh.java:129)
at com.example.android.myquake.Fesh.fesh(Fesh.java:39)
at com.example.android.myquake.Arrayloader.loadInBackground(Arrayloader.java:28)
at com.example.android.myquake.Arrayloader.loadInBackground(Arrayloader.java:18)
at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:312)
at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:57)
at android.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)
【问题讨论】:
-
处理网络调用的异常。
-
你在使用改造吗?
标签: java android android-broadcastreceiver