Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果Android中没有数据连接或wifi,如何显示警告消息?_Android_Http_Connection_Try Catch_Android Toast - Fatal编程技术网

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

如果Android中没有数据连接或wifi,如何显示警告消息?,android,http,connection,try-catch,android-toast,Android,Http,Connection,Try Catch,Android Toast,早上好 我有一个问题,关于观看祝酒词的通知失败连接。 如果启动应用程序无连接,我单击按钮打开连接到服务器的食谱列表,应用程序将崩溃,而不是保持活动状态,并呈现一个toast,提醒没有WiFi或数据连接。 我该怎么修理 import android.util.Log; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import

早上好

我有一个问题,关于观看祝酒词的通知失败连接。 如果启动应用程序无连接,我单击按钮打开连接到服务器的食谱列表,应用程序将崩溃,而不是保持活动状态,并呈现一个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)

发布错误消息我输入了日志错误