android - HttpResponse 响应 = httpclient.execute(httppost);即使使用 ASYNCTASK 也不能在 Android 4 中工作

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

我知道我不应该在 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

    }
}

最佳答案

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

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

表明你没有使用更多的变量。

所以你会这样做:

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

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

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

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

我假设您的 list 中有 INTERNET 权限。

唯一可以使用 ArrayIndexOutOfBounds 的地方是:

 String url2 = f_url[0];

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

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

关于android - HttpResponse 响应 = httpclient.execute(httppost);即使使用 ASYNCTASK 也不能在 Android 4 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13593818/

相关文章:

安卓工作室 2.3。我找不到 Cmake。安装 CMake 并同步项目

javascript - 在 react-native 中使用两个构造函数

Android:从回调中获取结果(联网KOUSH ION)

android - 三星 Android 设备不会进入 Debug模式

java - 将字符串 yyyy-MM-dd 转换为日期格式,电话语言从右到左(阿拉伯语)

java - 如何使用Asynctask建立socket连接?

Android 异步消费 Web 服务

json - 带有 JSON 数据的 SnapLogic REST Post

security - OAuth2 中密码授予类型 (ROPC) 的用途是什么?

php - 在 PHP 中调用后多次插入