在没有Internet连接的情况下加载本地JsonData

时间:2015-07-14 10:09:53

标签: java android json android-internet

我正在制作一个Android应用程序,我想在其中加载json数据。我的规格是,如果有可用的互联网,那么从我提供的网址加载数据。

如果没有互联网,请加载本地json.txt文件。

如果有互联网连接,我的应用程序可以从网址加载数据,如果绝对没有互联网连接,它可以加载本地json数据。

但是在这样的元状态下,我的应用程序崩溃了。enter image description here

日志

07-14 09:56:21.258  22499-28349/scientist.jobless.foodmana E/log_tag﹕ Error in http connection java.net.UnknownHostException: Unable to resolve host "lit-hamlet-6856.herokuapp.com": No address associated with hostname
07-14 09:56:21.258  22499-28349/scientist.jobless.foodmana E/log_tag﹕ Error converting result java.lang.NullPointerException
07-14 09:56:21.258  22499-28349/scientist.jobless.foodmana E/log_tag﹕ Error parsing data org.json.JSONException: End of input at character 0 of
07-14 09:56:21.266  22499-28349/scientist.jobless.foodmana E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:299)
            at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)
     Caused by: java.lang.NullPointerException
            at scientist.jobless.foodmana.JsonLoadSubFirstFirst$DownloadJSON.doInBackground(JsonLoadSubFirstFirst.java:239)
            at scientist.jobless.foodmana.JsonLoadSubFirstFirst$DownloadJSON.doInBackground(JsonLoadSubFirstFirst.java:215)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)
07-14 09:56:21.878  22499-22499/scientist.jobless.foodmana E/WindowManager﹕ Activity scientist.jobless.foodmana.JsonLoadSubFirstFirst has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@5374c12c that was originally added here
    android.view.WindowLeaked: Activity scientist.jobless.foodmana.JsonLoadSubFirstFirst has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@5374c12c that was originally added here
            at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
            at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
            at android.view.Window$LocalWindowManager.addView(Window.java:547)
            at android.app.Dialog.show(Dialog.java:277)
            at scientist.jobless.foodmana.MyCustomProgressDialog.show(MyCustomProgressDialog.java:43)
            at scientist.jobless.foodmana.JsonLoadSubFirstFirst$DownloadJSON.onPreExecute(JsonLoadSubFirstFirst.java:227)
            at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
            at android.os.AsyncTask.execute(AsyncTask.java:534)
            at scientist.jobless.foodmana.JsonLoadSubFirstFirst.onCreate(JsonLoadSubFirstFirst.java:74)
            at android.app.Activity.performCreate(Activity.java:5008)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

下面是我的java代码,我正在检查连接类型。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get the view from listview_main.xml
    setContentView(R.layout.listview_main);
    // Execute DownloadJSON AsyncTask

    ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

    if (connec != null && (
            (connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) ||
                    (connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED))) {
        new DownloadJSON(this).execute();
        //You are connected, do something online.

    } else if (connec != null && (
            (connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.DISCONNECTED) ||
                    (connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.DISCONNECTED ))) {

        //Not connected.
        Toast.makeText(getApplicationContext(), "You must be connected to the internet", Toast.LENGTH_LONG).show();
        new LocalDownloadJSON().execute();
    }

    else if (connec != null && (
            (connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.UNKNOWN) ||
                    (connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.UNKNOWN ))) {

        //i want to load local json data here
    }

}

我的问题是如何处理我在模拟器图片中显示的元连接状态。

我已经尝试了this 回答,但我在isNetworkAvailable(context)上收到错误,因为无法解析方法isNetworkAvailable。

这个代码还有吗?

(connec != null && (
             (connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.UNKNOWN) ||
                        (connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.UNKNOWN )))

LocalJsonDownload.java

private class LocalDownloadJSON extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pd = new ProgressDialog(JsonLoadSubFirstFirst.this);

            pd.setMessage("Loading...");
            pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);

            pd.setIndeterminate(true);
            pd.setCancelable(false);
            pd.show();
        }



        @Override
        protected Void doInBackground(Void... params) {
            // Create an array
            localarraylist = new ArrayList<HashMap<String, String>>();
            // Retrieve JSON Objects from the given URL address

            StringBuffer sb = new StringBuffer();
            BufferedReader br = null;

            try {
                br = new BufferedReader(new InputStreamReader(getAssets().open("localfood.txt")));
                String temp;
                while ((temp = br.readLine()) != null)
                    sb.append(temp);
            }
            catch (IOException e)
            {

                e.printStackTrace();
            }
            finally
            {
                try
                {
                    br.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }

            myjsonstring = sb.toString();

            try{
                JSONObject jsonObj = new JSONObject(myjsonstring);
                JSONArray Attendance = jsonObj.getJSONArray("events");

                for (int i = 0; i < Attendance.length(); i++)

                {

                    JSONObject a = Attendance.getJSONObject(i);

                    String Name = a.getString("Name");
                    String time = a.getString("Time");
                    String ingredients = a.getString("ingredients");
                    String Serves = a.getString("Serves");
                    String descr = a.getString("Description");


                    HashMap<String, String> contact = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    contact.put("Name", Name);
                    contact.put("Time", time);
                    contact.put("ingredients", ingredients);
                    contact.put("Serves", Serves);
                    contact.put("Description", descr);
                    // adding contact to contact list
                    localarraylist.add(contact);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }


        @Override
        protected void onPostExecute(Void args) {
            Toast.makeText(getApplicationContext(),"Loading local data dishes, please connect o internet to load new dishes",Toast.LENGTH_LONG).show();
            // Locate the listview in listview_main.xml
            //setContentView(R.layout.listview_main);
            listview = (ListView) findViewById(R.id.listview);
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(JsonLoadSubFirstFirst.this, localarraylist);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);
            // Close the progressdialog
            //  mProgressDialog.dismiss();

            //   textView.setVisibility(View.VISIBLE);
            pd.dismiss();

            super.onPostExecute(args);

        }
    }

3 个答案:

答案 0 :(得分:1)

当互联网可用时,您可以在加载json数据时执行一项操作,您可以将其保存到sqite中,并在下次加载时检查网络是否可用,如果网络不可用,则可以从sqlite获取json数据并解析很简单。你可以使用GreenDao来实现它非常快速和可靠的

答案 1 :(得分:0)

由于互联网连接,似乎没有正确下载json文件。因此,当您读取不完整的下载文件时,它会抛出“JSONException:输入结束于字符0”

在阅读下载的文件之前,您应该检查原始文件大小的下载文件大小。可以在HTTP标头中找到原始文件大小。

或者您在服务器上有无效的json文件。

希望你会发现它有用。

答案 2 :(得分:0)

您无法处理此连接状态。唯一的方法是捕获IOException。更好的方法是在DownloadJSON的doInBackground方法中捕获IOException和JSONException,然后执行LocalDownloadJSON

    // POST: RecurringTemplate/Create
    [HttpPost]
    public ActionResult Create(RecurringTemplate recurringTemplate)
    {
        try
        {
            _repo.AddRecurringTemplate(recurringTemplate);

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }   

编辑:  更好地解析onPostExecute中的Json

           @Html.LabelFor(model => model.Schedule.Build_Time)
           <span class="glyphicon glyphicon-time"></span>
           @Html.EditorFor(model => model.Schedule.Build_Time, new {htmlAttributes = new {@class = "form-control"}}) 

            @Html.LabelFor(model => model.Schedule.Call_Start_Time)

            <span class="glyphicon glyphicon-time"></span>
            @Html.EditorFor(model => model.Schedule.Call_Start_Time, new {htmlAttributes = new {@class = "form-control"}})
            @Html.ValidationMessageFor(model => model.Schedule.Call_Start_Time, "", new {@class = "text-danger"})

            @Html.LabelFor(model => model.Schedule.Call_End_Time)
            <span class="glyphicon glyphicon-time"></span>
            @Html.EditorFor(model => model.Schedule.Call_End_Time, new {htmlAttributes = new {@class = "form-control"}})