我正在使用 Asynctask 从网络中提取一些数据并将其用于进一步的计算。我也实现了连接检查,如果没有连接,它会返回 false,但如果有互联网连接并且“服务器已关闭”,它会崩溃。我想显示一个对话框来通知用户他们的互联网遇到问题。但我似乎无法正确处理。请帮忙。提前致谢。:)
HttpClient client;
String URL, re, element;
JSONObject json, getjson;
int i, j, statusCode;
HttpGet httpget;
HttpResponse response;
HttpEntity entity;
InputStream is;
BufferedReader reader;
StringBuilder sb, sb1;
SharedPreferences getinput, passjson;
ProgressDialog pd;
Context context = DataRetrieve.this;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.dataretrieve);
// Bundle gotBasket = getIntent().getExtras();
// URL = gotBasket.getString("key");
getinput = getSharedPreferences("input", 0);
URL = getinput.getString("key", null);
if (!isNetworkReachable()) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("No Network Connection");
builder.setMessage("The Network is unavailable. Please check your internet connection.");
builder.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent m = new Intent(
android.provider.Settings.ACTION_SETTINGS);
startActivity(m);
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Intent n = new Intent(DataRetrieve.this,
OpTilt.class);
startActivity(n);
}
});
builder.create().show();
} else {
Read r = new Read();
r.execute();
}
}
private boolean isNetworkReachable() {
// TODO Auto-generated method stub
ConnectivityManager mManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo current = mManager.getActiveNetworkInfo();
if (current != null && current.isConnectedOrConnecting()) {
return true;
}
return false;
}
public class Read extends AsyncTask<String, Integer, String> {
@Override
protected void onPreExecute() {
pd = new ProgressDialog(DataRetrieve.this);
pd.setTitle("Processing...");
pd.setMessage("Please wait.");
pd.setCancelable(false);
pd.setIndeterminate(true);
pd.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
re = null;
is = null;
json = null;
try {
httpget = new HttpGet(URL);
client = new DefaultHttpClient();
response = client.execute(httpget);
entity = response.getEntity();
is = entity.getContent();
statusCode = response.getStatusLine().getStatusCode();
} catch (Exception e) {
statusCode = -1;
Log.e("log_tag", "Erro http " + e.toString());
}
if (statusCode == 200) {
try {
reader = new BufferedReader(new InputStreamReader(is,
"UTF-8"), 8);
sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
re = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Erro conversão " + e.toString());
}
}
return re;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
pd.dismiss();
try {
json = new JSONObject(result);
getjson = json.getJSONObject("Solar");
String H[] = new String[getjson.length()];
for (i = 0, j = 1; i < getjson.length(); i++, j++) {
H[i] = getjson.getString("" + j);
}
SharedPreferences sendHm;
sendHm = getSharedPreferences("parsedata", 0);
SharedPreferences.Editor editor = sendHm.edit();
for (int k = 0; k < H.length; k++) {
editor.putString("Hm" + k, H[k]);
}
editor.commit();
Intent f = new Intent(DataRetrieve.this, Calculator.class);
startActivity(f);
/*
* Bundle bundle = new Bundle(); bundle.putStringArray("key1",
* H); Intent f = new Intent(DataRetrieve.this,
* Calculator.class); f.putExtras(bundle); startActivity(f);
*/
}
catch (JSONException e) {
Log.e("log_tag", "Erro dados " + e.toString());
}
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}