我创建了以下活动,其中包括从远程url下载json数据的asynctask。我添加了一个AlertDialog来检查用户设备上的Internet连接是否可用,并添加了一个超时来处理慢速Internet连接。
以下是我的活动:
public class MyActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_acitivity);
if (isNetworkAvailable(this))
new MyAsyncTask().execute();
else {
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setTitle("Error");
builder.setMessage("No internet connection");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
finish();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
private class MyAsyncTask extends AsyncTask<String, String, String> {
RelativeLayout progress;
@Override
protected void onPreExecute() {
super.onPreExecute();
progress = (RelativeLayout) findViewById(R.id.progressBarlayout);
progress.setOnClickListener(null);
progress.setVisibility(View.VISIBLE);
}
@Override
protected String doInBackground(String... params) {
int timeout = 120;
String jsonString = null;
BasicHttcat1arams basicParams = new BasicHttcat1arams();
HttpConnectionParams.setConnectionTimeout(basicParams, timeout * 1000);
HttpConnectionParams.setSoTimeout(basicParams, timeout * 1000 );
DefaultHttpClient client = new DefaultHttpClient(basicParams);
HttpGet request = new HttpGet("https://www.example.com/data.json");
request.addHeader("Cache-Control", "no-cache");
try {
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
InputStreamReader in = new InputStreamReader(entity.getContent());
BufferedReader reader = new BufferedReader(in);
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = reader.readLine()) != null) {
sb.acat1end(line);
}
jsonString = sb.toString();
} catch (SocketTimeoutException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return jsonString;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
JSONObject json;
try {
json = new JSONObject(s);
p1 = Double.parseDouble(json.getJSONObject("cat1").getJSONObject("subcat1").getString("rcv"));
p2 = Double.parseDouble(json.getJSONObject("cat2").getJSONObject("subcat1").getString("rcv"));
p3 = Double.parseDouble(json.getJSONObject("cat1").getJSONObject("subcat1").getString("avg"));
p4 = Double.parseDouble(json.getJSONObject("cat2").getJSONObject("subcat1").getString("avg"));
p5 = Double.parseDouble(json.getJSONObject("cat1").getJSONObject("subcat2").getString("status"));
p6 = Double.parseDouble(json.getJSONObject("cat2").getJSONObject("subcat2").getString("status"));
} catch (JSONException e) {
e.printStackTrace();
}
progress.setVisibility(View.GONE);
}
}
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
}
它工作,但我的应用程序崩溃,例如,如果远程url不可用,或在下载网络或wifi离线。
这是Logcat:
03-02 15:00:22.466: E/AndroidRuntime(23478): FATAL EXCEPTION: main
03-02 15:00:22.466: E/AndroidRuntime(23478): Process: com.myapp, PID: 23478
03-02 15:00:22.466: E/AndroidRuntime(23478): java.lang.NullPointerException
03-02 15:00:22.466: E/AndroidRuntime(23478): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
03-02 15:00:22.466: E/AndroidRuntime(23478): at org.json.JSONTokener.nextValue(JSONTokener.java:94)
03-02 15:00:22.466: E/AndroidRuntime(23478): at org.json.JSONObject.<init>(JSONObject.java:155)
03-02 15:00:22.466: E/AndroidRuntime(23478): at org.json.JSONObject.<init>(JSONObject.java:172)
03-02 15:00:22.466: E/AndroidRuntime(23478): at com.myapp.MyActivity$MyAsyncTask.onPostExecute(MyActivity.java:377)
03-02 15:00:22.466: E/AndroidRuntime(23478): at com.myapp.MyActivity$MyAsyncTask.onPostExecute(MyActivity.java:1)
03-02 15:00:22.466: E/AndroidRuntime(23478): at android.os.AsyncTask.finish(AsyncTask.java:632)
03-02 15:00:22.466: E/AndroidRuntime(23478): at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-02 15:00:22.466: E/AndroidRuntime(23478): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
03-02 15:00:22.466: E/AndroidRuntime(23478): at android.os.Handler.dispatchMessage(Handler.java:102)
03-02 15:00:22.466: E/AndroidRuntime(23478): at android.os.Looper.loop(Looper.java:136)
03-02 15:00:22.466: E/AndroidRuntime(23478): at android.app.ActivityThread.main(ActivityThread.java:5146)
03-02 15:00:22.466: E/AndroidRuntime(23478): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 15:00:22.466: E/AndroidRuntime(23478): at java.lang.reflect.Method.invoke(Method.java:515)
03-02 15:00:22.466: E/AndroidRuntime(23478): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
03-02 15:00:22.466: E/AndroidRuntime(23478): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
03-02 15:00:22.466: E/AndroidRuntime(23478): at dalvik.system.NativeStart.main(Native Method)
如何处理这些事件?
最佳答案
在onpostexecute中创建json对象之前,应该检查字符串s是否为空。如果不为空,则继续执行您正在执行的操作;如果为空,则重新检查Internet连接并重试下载或任何您想要的操作。