如果在Android中没有数据连接或wifi,如何显示警告消息?

时间:2015-09-25 06:59:25

标签: android http connection try-catch android-toast

早上好,

关于查看连接失败的通知,我遇到了问题。 如果您启动应用程序无连接,我单击按钮打开我附加到服务器的配方列表,应用程序崩溃,而不是保持活动状态并呈现警报,提示没有WiFi或数据连接。 我该如何解决?

import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.IOException;
import java.nio.channels.NoConnectionPendingException;
import java.nio.channels.NotYetConnectedException;
import java.util.List;

public class Connector {
public JSONArray ApiRecipes()    {
// URL for getting all Recipes
String url = "example.com/Recipes.php";
// Get HttpResponse Object from url.
// Get HttpEntity from Http Response Object
HttpEntity httpEntity = null;
try
{
    DefaultHttpClient httpClient = new DefaultHttpClient();  // Default HttpClient
    HttpGet httpGet = new HttpGet(url);
    HttpResponse httpResponse = httpClient.execute(httpGet);
    httpEntity = httpResponse.getEntity();
} catch (ClientProtocolException e) {
    // Signals error in http protocol
    e.printStackTrace();
    //Log Errors Here
} catch (IOException e) {
    e.printStackTrace();
}
// Convert HttpEntity into JSON Array
JSONArray jsonArray = null;
if (httpEntity != null) {
    try {
        String entityResponse = EntityUtils.toString(httpEntity);
        Log.e("Entity Response  : ", entityResponse);
        jsonArray = new JSONArray(entityResponse);
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
return jsonArray;
}

我输入了日志错误

报告错误:

09-25 09:29:39.510  26962-26962/com.project.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.project.app, PID: 26962
java.lang.NullPointerException
        at com.project.app.recipes.recipesRicette.getCount(recipesRicette.java:31)
        at android.widget.ListView.setAdapter(ListView.java:480)
        at com.project.app.recipes.RecipesActivity.setListAdapter(RecipesActivity.java:67)
        at com.project.app.recipes.RecipesActivity$getAll.onPostExecute(RecipesActivity.java:82)
        at com.project.app.recipes.RecipesActivity$getAll.onPostExecute(RecipesActivity.java:70)
        at android.os.AsyncTask.finish(AsyncTask.java:632)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5013)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案