0

当用户中断到数据馈送的传输时,我的 Android 应用程序会null强制关闭。或者有时如果互联网速度很慢而且似乎停滞不前,我会得到null

这是我处理http代码的方式:

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url_select);

        try {
            httpPost.setEntity(new UrlEncodedFormEntity(param));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();

            // read content
            is = httpEntity.getContent();

        } catch (Exception e) {

            Log.e("log_tag", "Error in http connection " + e.toString());
        }
        try {
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line = "";
            while ((line = br.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();

        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }

        return null;
    }

然后在onPostExecute, AsyncTasknull 在这里返回,如下面的 LogCat 所示:

adapter = new ReviewAdapter(getActivity(), r, tf); // Line 1164

我想r是什么null杀死了它?(tf是我传入的TrueType字体)

这是r代码:

        for (String i : reviews) {
            r[index] = new Review(reviews.get(index),
                    datelist.get(index), userlist.get(index),
                    itemlist.get(index), ratings.get(index),
                    helpfulCount.get(index), chatCounts.get(index),
                    catlist.get(index), titlelist.get(index));
            index++;
        }

这些ArrayLists是通过JSONParsing fromPHP等填充的。

所以我的问题是:我如何普遍处理这个问题?有没有办法避免null?或者我怎样才能优雅地抛出错误而不是强制关闭?

这是Logcat:

02-19 12:15:12.012: E/AndroidRuntime(32116): FATAL EXCEPTION: main
02-19 12:15:12.012: E/AndroidRuntime(32116): java.lang.NullPointerException
02-19 12:15:12.012: E/AndroidRuntime(32116):    at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
02-19 12:15:12.012: E/AndroidRuntime(32116):    at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
02-19 12:15:12.012: E/AndroidRuntime(32116):    at com.---.---.ReviewAdapter.<init>(ReviewAdapter.java:42)
02-19 12:15:12.012: E/AndroidRuntime(32116):    at com.---.---.RateFragmentActivity$ReviewFragment$ReviewTask.onPostExecute(RateFragmentActivity.java:1164)
02-19 12:15:12.012: E/AndroidRuntime(32116):    at com.---.---.RateFragmentActivity$ReviewFragment$ReviewTask.onPostExecute(RateFragmentActivity.java:1)
02-19 12:15:12.012: E/AndroidRuntime(32116):    at android.os.AsyncTask.finish(AsyncTask.java:631)
02-19 12:15:12.012: E/AndroidRuntime(32116):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-19 12:15:12.012: E/AndroidRuntime(32116):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-19 12:15:12.012: E/AndroidRuntime(32116):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 12:15:12.012: E/AndroidRuntime(32116):    at android.os.Looper.loop(Looper.java:137)
02-19 12:15:12.012: E/AndroidRuntime(32116):    at android.app.ActivityThread.main(ActivityThread.java:5193)
02-19 12:15:12.012: E/AndroidRuntime(32116):    at java.lang.reflect.Method.invokeNative(Native Method)
02-19 12:15:12.012: E/AndroidRuntime(32116):    at java.lang.reflect.Method.invoke(Method.java:511)
02-19 12:15:12.012: E/AndroidRuntime(32116):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
02-19 12:15:12.012: E/AndroidRuntime(32116):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
02-19 12:15:12.012: E/AndroidRuntime(32116):    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

如果第一个try块抛出异常,您可能应该停在那里,不要打扰第二个try块,因为is它可能不是httpEntity.getContent();if throw an exception 的值httpClient.execute(在连接不良的情况下它会这样做。

为什么你不能只检查是否rnull并向用户显示他们的连接丢失的错误?

于 2013-02-19T20:36:24.520 回答