Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java httpPost未使用asynctask工作-提供无效索引,大小为0异常_Java_Android_Exception_Http Post - Fatal编程技术网

Java httpPost未使用asynctask工作-提供无效索引,大小为0异常

Java httpPost未使用asynctask工作-提供无效索引,大小为0异常,java,android,exception,http-post,Java,Android,Exception,Http Post,为了避免在UI线程中执行与http相关的事情,我在asynctask中迁移了我的代码,在此之前,它在3.0之前的版本上工作得很好——但是,在asynctask中复制粘贴代码之后,它开始给出无效索引,大小为0异常。每当我需要使用该方法时,我都会应用调用-- newdataretrievalviaaasynctask().execute(url,null,null)-- 下面怎么了 class dataRetrievalViaAsyncTask extends AsyncTask<String

为了避免在UI线程中执行与http相关的事情,我在asynctask中迁移了我的代码,在此之前,它在3.0之前的版本上工作得很好——但是,在asynctask中复制粘贴代码之后,它开始给出
无效索引,大小为0异常
。每当我需要使用该方法时,我都会应用调用--

newdataretrievalviaaasynctask().execute(url,null,null)--

下面怎么了

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

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
    }

    @Override
    protected Void 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);   
        }
        // convert response to string
        return null;


    }
    protected void onPostExecute()
    {
        try
        {
            Log.i("tag","before : BufferedReader reader = new BufferedReader(new Inp");
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");

            String line = "0";
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        } catch (Exception e)
        {
                Log.e("log_tag", "Error in http connection", e);   
        }

        try
        {
            Log.i("tag", "before : jsons ");
            jArray = new JSONArray(result);
            JSONObject json_data = null;

            Log.i("tag", Integer.toString(jArray.length()));

            for (int i = 0; i < jArray.length(); i++)
            {
                json_data = jArray.getJSONObject(i);

                uid = json_data.getInt("uid");
                item1= json_data.getString("item1");
                item2 = json_data.getString("item2");
                item3 = json_data.getString("item3");
                item4 = json_data.getString("item4");
                item5 = json_data.getString("item5");
                item6 = json_data.getString("item6");
                favorited = json_data.getString("favorited");

                currentList.add(new itemClass(uid, item1 item2)); //there is a constructor for this in the itemClass

                itemClass toSendToOffline = new itemsClass(uid, item1, item2, item3, item4, item5, item6, favorited);
                myDBHelper.insertFromOnlineToDBtoSendToOffline();

            }



        } catch (JSONException e1)
        {
            Toast.makeText(getBaseContext(), "Not Found", Toast.LENGTH_LONG).show();
        } catch (ParseException e1)
        {
            e1.printStackTrace();
        }
        super.onPostExecute(null);
    }
}

如果您有一些问题,希望这会有所帮助,请在这里发布,谢谢……

我看不到任何地方初始化的nameValuePairs变量,这实际上是造成问题的原因。

您能从LogCat发布错误跟踪吗?我想这行httppost.setEntity(新的UrlEncodedFormEntity(nameValuePairs))中有问题;请检查php文件中的您的post值。您的asyncTask中没有定义您的nameValuePairs,这可能会导致您在执行过程中出现问题。根据经验,尽量不要在
doInBackground
方法中访问UIThread中引用的对象,例如,该方法中没有定义nameValuePairs,或者它是作为参数传递的--@raju同样的代码在asynctask之外和4.0之前的android版本中都可以正常工作。但这并不能回答这个问题。若要评论或要求作者澄清,请在其帖子下方留下评论-您可以随时对自己的帖子发表评论,一旦您有足够的评论,您就可以发表评论。它们在主类中初始化。。。在oncreate方法之前。经过一些修改。。。结果是我试图访问arraylist,他们试图以比填充更快的速度填充。。。因此,这是一个错误。。然而,我做了一些改变。。。现在发现onPostExecute没有被执行。。可能是什么问题@在您的代码中,您并没有重写onPostExecute方法,就像您重写了其余两个方法一样。试试这个,在这个方法之前写@override,然后告诉我这行吗?
HttpResponse response = httpclient.execute(httppost);
   class dataRetrievalViaAsyncTask extends AsyncTask<Void, Void, String>
{
    String URL = "";

    public dataRetrievalViaAsyncTask( String url )
        {
            URL = url;
        }

    @Override
    protected void onPreExecute()
        {
        }

    @Override
    protected String doInBackground(Void... f_url)
        {
            String result="";
            try
                {
                    result=fetchdataFromServer(URL);
                }
            catch (JSONException e)
                {
                    e.printStackTrace();
                }
            return result;
        }

    protected void onPostExecute(String result)
        {
            // See your results as string //result
        }

    public JSONObject getJsonObjectToRequestToServer(String plid) throws JSONException
        {
            JSONObject parms = new JSONObject();
            parms.put("user_id", "");
            parms.put("app_key", "xyz");
            parms.put("secret", "abc");
            parms.put("token", "");
            parms.put("playurl", "1");
            parms.put("mode", "playlistdetail");
            parms.put("playlist_id", plid);
            return parms;
        }

    public String fetchdataFromServer(String url) throws JSONException
        {
            String stringresponce = null;
            try
                {
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(URL);

                    JSONObject parms = getJsonObjectToRequestToServer("1");

                    StringEntity se;

                    se = new StringEntity(parms.toString());
                    httpPost.setEntity(se);

                    httpPost.setHeader("Content-type", "application/json");

                    @SuppressWarnings("rawtypes")
                    ResponseHandler responseHandler = new BasicResponseHandler();
                    stringresponce = httpClient.execute(httpPost, responseHandler);
                }
            catch (UnsupportedEncodingException e)
                {
                    e.printStackTrace();
                }
            catch (ClientProtocolException e)
                {
                    e.printStackTrace();
                }
            catch (IOException e)
                {
                    e.printStackTrace();
                }
            return stringresponce;
        }
    }
dataRetrievalViaAsyncTask asyncTask=new dataRetrievalViaAsyncTask(Yoururl);
asyncTask.execute();