asynctask在英雄2.1上返回null

时间:2012-08-02 20:23:03

标签: android android-asynctask

我是新手,所以忍受我。

我有一个asynctask,可以从Web服务中提取我附近最近的位置。它适用于大于2.1的新Android设备。我有一个2.1的HTC英雄,每次按下按钮查找数据时都会崩溃。

这是我的代码。第一部分从设备获取lat和long,并将其发送到我在服务器上创建的php脚本。

    private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        //String response = "";
        for (String url : urls) {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            try {
                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("lat", lat));
                nameValuePairs.add(new BasicNameValuePair("long", longit));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                // Execute HTTP Post Request
                UrlEncodedFormEntity ent = new UrlEncodedFormEntity(nameValuePairs,
                        HTTP.UTF_8);
                httppost.setEntity(ent);
                HttpResponse responsePOST = httpclient.execute(httppost);
                responseBody = EntityUtils.toString(responsePOST.getEntity());
                Log.i("YAhh", responseBody);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }
        }
        return responseBody;
        }

此部分将数据发送到另一个要解析的活动并显示在列表视图中。

  @Override
    protected void onPostExecute(String result) {
        Log.i("returndata",responseBody);
    /*  Intent intent = new Intent(HomeActivity.this,
                locationActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("returnBody", responseBody);
        bundle.putString("latit", lat);
        bundle.putString("longit" + "", longit);
        intent.putExtras(bundle);
        startActivity(intent);*/
    }
}

这是我收到的错误,据我所知,没有返回数据,因此空值导致错误。

  08-02 15:17:13.848: E/AndroidRuntime(1117): Uncaught handler: thread main exiting due to uncaught   exception
  08-02 15:17:13.968: E/AndroidRuntime(1117): java.lang.NullPointerException: println needs a message
  08-02 15:17:13.968: E/AndroidRuntime(1117):   at android.util.Log.println(Native Method)
  08-02 15:17:13.968: E/AndroidRuntime(1117):   at android.util.Log.i(Log.java:136)
  08-02 15:17:13.968: E/AndroidRuntime(1117):   at com.mlgw.mlgwapp.HomeActivity$DownloadWebPageTask.onPostExecute(HomeActivity.java:278)
  08-02 15:17:13.968: E/AndroidRuntime(1117):   at   com.mlgw.mlgwapp.HomeActivity$DownloadWebPageTask.onPostExecute(HomeActivity.java:1)
  08-02 15:17:13.968: E/AndroidRuntime(1117):   at android.os.AsyncTask.finish(AsyncTask.java:417)
  08-02 15:17:13.968: E/AndroidRuntime(1117):   at android.os.AsyncTask.access$300(AsyncTask.java:127)
  08-02 15:17:13.968: E/AndroidRuntime(1117):   at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
  08-02 15:17:13.968: E/AndroidRuntime(1117):   at android.os.Handler.dispatchMessage(Handler.java:99)
  08-02 15:17:13.968: E/AndroidRuntime(1117):   at android.os.Looper.loop(Looper.java:123)
  08-02 15:17:13.968: E/AndroidRuntime(1117):   at android.app.ActivityThread.main(ActivityThread.java:4595)
  08-02 15:17:13.968: E/AndroidRuntime(1117):   at java.lang.reflect.Method.invokeNative(Native Method)
  08-02 15:17:13.968: E/AndroidRuntime(1117):   at java.lang.reflect.Method.invoke(Method.java:521)
  08-02 15:17:13.968: E/AndroidRuntime(1117):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
  08-02 15:17:13.968: E/AndroidRuntime(1117):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
  08-02 15:17:13.968: E/AndroidRuntime(1117):   at dalvik.system.NativeStart.main(Native Method)

有没有人知道为什么这不能用于2.1? 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

您以错误的方式使用了异步任务。

首先,responseBody在哪里宣布? 确保它不是空的!!

另一个错误是: 你在doInBackground中返回responseBody。 该返回字符串是resultonPostExecute方法。

依据onPostExecuteresponseBody替换为result ..

  Log.i("returndata",result);

...

答案 1 :(得分:0)

您希望将Log.w()printStackTrace()放入例外:

   } catch (ClientProtocolException e) {
        e.printStackTrace();
   } catch (IOException e) {
        e.printStackTrace();
   }
 }
 return responseBody;
 }

您可以使用logcat查看导致这些问题的原因。

此方法onPostExecute看起来很可疑。将responseBody替换为result

  @Override
    protected void onPostExecute(String result) {
         Log.i("returndata",result);

    }