迫使Json和可怜的互联网

时间:2015-12-12 14:40:43

标签: android json android-asynctask

在我的项目中打开应用程序AsyncTask启动并从json页面获取数据时我添加if for check network是否可用。但是如果网络访问和启动AsyncTask网络后差或关闭突然加力关闭。这是JsonParser类:

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";
    public Context mContext;

    // constructor
    public JSONParser(Context _context) {
        mContext = _context;
    }
    public JSONObject getJSONFromUrl(String url) {
        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
        // return JSON String
        return jObj;
    }

}

和AsyncTask是这样的:

public class myAsyncTask extends AsyncTask<String ,String,JSONObject> {

    @Override
    protected void onPreExecute(){
        if(b == 0)
        _func.showProgress("on");

    }

    @Override
    protected JSONObject doInBackground(String... params) {
        jParser = new JSONParser(mContext);
        jObject = jParser.getJSONFromUrl(url);
        return jObject;
    }

    @Override
    protected void onPostExecute(JSONObject jObject){
        try {
            if (jObject.getString("code").equals("200")) {

                if (b == 0)
                    _func.showProgress("off");

                try {
                    jArray = jObject.getJSONArray("result");
                    for (int i = 0; i < jArray.length(); i++) {
                        JSONObject rItem = jArray.getJSONObject(i);
                        title.add(rItem.getString("title").toString());
                        id.add(rItem.getString("id").toString());
                    }

                } catch (JSONException e) {
                    _func.toast(mContext.getString(R.string.nothingToShow));
                }


                }

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

    }

当互联网变得糟糕或正在关闭时,我如何能够停止asyncTask ??? :(

12-12 18:18:03.940 15313-15339/ir.jobcenterplace.app.jobfinder E/Buffer Error: Error converting result java.lang.NullPointerException: lock == null
12-12 18:18:03.940 15313-15339/ir.jobcenterplace.app.jobfinder E/JSON Parser: Error parsing data org.json.JSONException: End of input at character 0 of 
12-12 18:18:03.940 15313-15313/ir.jobcenterplace.app.jobfinder D/AndroidRuntime: Shutting down VM
12-12 18:18:03.940 15313-15313/ir.jobcenterplace.app.jobfinder W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x4187ada0)
12-12 18:18:03.940 15313-15313/ir.jobcenterplace.app.jobfinder E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: ir.jobcenterplace.app.jobfinder, PID: 15313
                                                                                 java.lang.NullPointerException
                                                                                     at ir.jobcenterplace.app.jobfinder.myAsyncTask.onPostExecute(myAsyncTask.java:87)
                                                                                     at ir.jobcenterplace.app.jobfinder.myAsyncTask.onPostExecute(myAsyncTask.java:24)
                                                                                     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:146)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5679)
                                                                                     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:1291)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
                                                                                     at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案