变量返回null,外面的AsyncTask的android [英] variable returns null outside asynctask android

查看:201
本文介绍了变量返回null,外面的AsyncTask的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有过PHP获取数据的AsyncTask的。该AsyncTask的做工精细,它传递的值,以一个全球性的数组列表。然而,当我尝试.execute()之后调用上的onCreate变量,它返回一个空尺寸/值(S)。我想知道为什么当它应该有一个值这个全局变量返回null。在此先感谢!

这是code为asyntask:

 私有类get_pendingreq扩展的AsyncTask<字符串,整数,字符串>
 {
    @覆盖
    保护字符串doInBackground(字符串...为arg0)
    {
        InputStream为= NULL;
        字符串结果=;        尝试
        {
            HttpClient的HttpClient的=新DefaultHttpClient();
            HttpPost httppost =新HttpPost(URL +pending_requests.php);            清单<&的NameValuePair GT;参数=新的ArrayList<&的NameValuePair GT;();
            parameter.add(新BasicNameValuePair(电子邮件,globalVariables.accountemail));
            httppost.setEntity(新UrlEn codedFormEntity(参数));            HTT presponse响应= httpclient.execute(httppost);
            HttpEntity实体= response.getEntity();
            是= entity.getContent();        }赶上(例外五)
        {
            Log.e(log_tag,在HTTP连接错误+ e.toString());
        }        尝试
        {
            读者的BufferedReader =新的BufferedReader(
                    新InputStreamReader的(是ISO-8859-1),8);
            StringBuilder的SB =新的StringBuilder();
            串线= NULL;            而((行= reader.readLine())!= NULL)
            {
                sb.append(行+\\ n);
            }
            is.close();
            结果= sb.toString();
        }赶上(例外五)
        {
            Log.e(log_tag,错误转换结果+ e.toString());
        }        返回结果;
    }    @覆盖
    保护无效onPostExecute(字符串结果)
    {
        super.onPostExecute(结果);        尝试
        {
            / * JSON解析从这里开始* /
            JSONObject的jArray =新的JSONObject(结果);
            /*globalVariables.pendingdate =新的ArrayList<串GT;(); * /
            JSONArray请求= jArray.getJSONArray(请求);            的for(int i = 0; I< request.length();我++)
            {                JSONObject的E = request.getJSONObject(I)                / *把值的ArrayList<串GT; * /
                globalVariables.pendingdate.add(e.getString(DATE));            }            Log.d(striiing,globalVariables.pendingdate.toString());
        }赶上(JSONException E)
        {
            e.printStackTrace();
                }
    }
}

主要活动:

 公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.calendar);    新get_pendingreq()执行();    System.out.print(alalala+ globalVariables.pendingdate.size());
      } //返回null

全局变量:

 公共静态的ArrayList<串GT; pendingdate =新的ArrayList<弦乐取代;


解决方案

由于的AsyncTask 没有完成。把 System.out.print(完成); onPostExcute(),看看谁最先打印

I have an asynctask which gets its data through php. The asynctask works fine, it passes the values to a global arraylist. However, when i try to call the variable on onCreate after .execute(), it returns a null size/value(s). I would like to know why this global variable returns a null when its supposed to have a value. Thanks in advance!

this is the code for asyntask:

 private class  get_pendingreq extends AsyncTask<String, Integer, String> 
 {
    @Override
    protected String doInBackground(String... arg0) 
    {
        InputStream is = null;
        String result = "";

        try 
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url+"pending_requests.php");

            List<NameValuePair> parameter = new ArrayList<NameValuePair>();
            parameter.add(new BasicNameValuePair("email", globalVariables.accountemail));


            httppost.setEntity(new UrlEncodedFormEntity(parameter));

            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

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

        try 
        {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;

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

        return result;
    }

    @Override
    protected void onPostExecute(String result) 
    {
        super.onPostExecute(result);

        try 
        {
            /*  JSON parsing starts here    */
            JSONObject jArray = new JSONObject(result);
            /*globalVariables.pendingdate = new ArrayList<String>();*/
            JSONArray request = jArray.getJSONArray("request");

            for (int i = 0; i < request.length(); i++) 
            {

                JSONObject e = request.getJSONObject(i);

                /* puts values to arraylist<String>  */                 
                globalVariables.pendingdate.add(e.getString("date"));

            }

            Log.d("striiing", globalVariables.pendingdate.toString());
        } catch (JSONException e) 
        {
            e.printStackTrace();
                }
    }
}

Main activity:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.calendar);

    new get_pendingreq().execute();

    System.out.print("alalala" + globalVariables.pendingdate.size());
      }//this returns null

Global Variable:

public static ArrayList<String> pendingdate = new ArrayList<String>;

解决方案

Because AsyncTask not finished. Put System.out.print("finish"); in onPostExcute() and see who is printed first.

这篇关于变量返回null,外面的AsyncTask的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆