【问题标题】:HttpResponse response = httpclient.execute(httppost); not working in Android 4 EVEN WITH ASYNCTASKHttpResponse 响应 = httpclient.execute(httppost);即使使用 ASYNCTASK 也无法在 Android 4 中工作
【发布时间】:2012-11-27 21:53:09
【问题描述】:

我知道我不应该在 UI 线程上运行与网络相关的东西,因此我正在使用 asynctask 通过远程服务器上的 php 从数据库中读取一些东西。 但是,当代码到达 --HttpResponse response = httpclient.execute(httppost);-- 我收到错误 -- java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 -- 因此代码不起作用。

请注意,我这样调用 asyncTask -- new dataRetrievalViaAsyncTask().execute(url,url,url); (第二个和第三个“url”是虚拟的,因为它们没有被使用)——在 oncreate() 中。

那里有什么问题?

class dataRetrievalViaAsyncTask extends AsyncTask<String, String, String>
{
    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... f_url)
    {
        Log.i("tag", "inside doInBackground");
        String url2 = f_url[0];
        Log.i("tag", url2);

        HttpClient httpclient = new DefaultHttpClient();
        Log.i("tag",    "done : HttpClient httpclient = new DefaultHttpClient();");

        HttpPost httppost = new HttpPost(url2);
        Log.i("tag", "done : HttpPost httppost = new HttpPost(url);");

        try
        {

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            Log.i("tag",    "done : httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));");
            HttpResponse response = httpclient.execute(httppost);
            Log.i("tag",    "done : HttpResponse response = httpclient.execute(httppost);");
            HttpEntity entity = response.getEntity();
            Log.i("tag",    "done : HttpEntity entity = response.getEntity();");
            is = entity.getContent();
            Log.i("tag", "after : is = entity.getContent();");

        } catch (Exception e)
        {
            Log.e("log_tag", "Error in http connection" + e.toString());
        }
        // convert response to string
        return "a";

    }

    @Override
    protected void onPostExecute(String result)
    {
               // enter code here

    }
}

【问题讨论】:

  • 您的LogCat 从这一行生成的Log.i("tag", url2); 输出是否为您提供了正确的网址?您能否将您的LogCat 输出添加到您的问题中。
  • 丹,你能找到解决这个问题的方法吗?我也面临同样的问题。如果您解决了这个问题,请告诉我。
  • 我需要关闭 DAN :joysob:

标签: android android-asynctask http-post runtime-error


【解决方案1】:

为了避免进一步混淆,请执行以下操作:

class dataRetrievalViaAsyncTask extends AsyncTask<String, Void, Void>
{

显示您没有使用其他变量。

所以你会这样做:

 dataRetrievalViaAsyncTask().execute(url, null, null);

然后在您的 catch 块中将其更改为:

 catch (Exception e)
    {
        Log.e("log_tag", "Error in http connection", e);
    }

然后您将获得正确的堆栈跟踪,希望能够使用类/方法名称和行号进行调试。

我假设你的清单中有 INTERNET 权限。

您可以拥有 ArrayIndexOutOfBounds 的唯一地方是:

 String url2 = f_url[0];

意味着您没有将字符串 URL 正确发送到 ASyncTask。

另一个问题是您正在使用nameValuePairs 变量,但不要向我们展示它是如何实例化的。我会赌是你的问题。

【讨论】:

  • 意味着您没有将字符串 URL 正确发送到 ASyncTask。 nameValuePairs = new ArrayList();是的,我在清单中有互联网许可。否则它会从第一条网络相关行而不是 httppost 给出错误。
  • 您是否更改了它以便用行号记录实际错误?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-02
  • 2011-07-03
  • 1970-01-01
  • 2015-09-15
  • 2021-10-27
相关资源
最近更新 更多