我设计了一个应用程序,在发出http请求之前检查互联网连接并且工作正常,但问题是当有互联网连接并且我切换移动数据或在此期间强行丢失互联网连接时搜索数据,应用程序崩溃。 有没有办法让我的应用程序报告没有互联网连接,如果互联网连接丢失,同时获取数据,我检查并尝试使用广播接收器,但在Android清单中我们要声明意图过滤器,我不能找到这些 我也使用加载器而不是异步任务来执行网络请求 换句话说
我只需要一种方法来处理 响应验证
该应用程序会检查设备是否已连接到互联网并做出相应的响应。验证请求的结果是为了解决服务器响应错误或服务器响应不足可能是由于在获取数据期间丢失了互联网连接
STATUS_BREAKPOINT
//这是获取数据的代码
公共课Fesh {
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);
答案 0 :(得分:0)
您可以将广播的操作声明为:
<receiver android:name=".Receiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
在广播接收器内部,当连接发生变化时,将调用onReceive。
根据更改,您可以显示错误或继续获取数据。