我想問一個對你們中的一些人來說可能很簡單的問題。Android應用程序找出是否有連接
我怎麼知道我的應用程序是否有互聯網連接?
我想連接到MySQL數據庫,當沒有Internet時,應該有一個AlertDialog。
new AsyncTask() {
ProgressDialog dialog = ProgressDialog.show(AppActivity.this, "Lade", "Daten werden abgerufen...", true);
@Override
protected void onPostExecute(Object result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.dismiss();
max = Name.size()-1;
kontrolle = new boolean [max*2];
pb.setMax(max/2);
java.util.Arrays.fill(kontrolle, false);
bilder();
}
@Override
protected Object doInBackground(Object... arg0)
{
// TODO Auto-generated method stub
dialog.show();
getData();
return null;
}
}.execute();
private void getData()
{
String result = "";
ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
try
{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://.../read.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is=entity.getContent();
}
catch(Exception e)
{
Log.e("log-tag","Keine Verbindung"+e.toString());
Toast.makeText(getApplicationContext(), "Keine Verbindung!", Toast.LENGTH_SHORT).show();
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = "";
while((line= reader.readLine())!=null)
{
sb.append(line+"n");
}
is.close();
result = sb.toString();
result.trim();
}
catch(Exception e)
{
Log.e("log-tag","Error"+e.toString());
}
try
{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++)
{
JSONObject json_data = jArray.getJSONObject(i);
ID.add((String) json_data.get("id"));
Name.add((String) json_data.get("name"));
Pts.add((String)json_data.get("pts"));
}
}
catch(Exception e)
{
Log.e("log-tag","Error2"+e.toString());
}
}
我該如何做到這一點?我不知道 我認爲這
catch(Exception e)
{
Log.e("log-tag","Keine Verbindung"+e.toString());
Toast.makeText(getApplicationContext(), "Keine Verbindung!", Toast.LENGTH_SHORT).show();
}
會做的工作,但是當我沒有連接(因爲我把我在飛行模式下手機)也僅僅是一個無止境的ProgressDialog :(
重複問題在這裏http://stackoverflow.com/questions/9570237/android-check-internet-connection – 2012-07-06 09:50:32