使用asyncTask时的NullPointer

时间:2014-04-01 10:39:29

标签: php android json android-asynctask

我开始在Android中编程,我正在做一个连接到服务器中的PHP文件的应用程序,然后用JSON读取数据。该应用程序几乎正常工作。它从文件中读取JSON数据,我可以在" onPostExecute"中插入带有JSON值的文本。处理程序。

protected void onPostExecute(String string) {
        pDialog.dismiss();
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */

                txt1.setText(jo.toString() + "  " + jo.length());
                try {
                    //Creating an array from the JSON Object
                    jarray = jo.getJSONArray("Anunciante");
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


它有效,但在第一次执行之后,我无法使用JSONObject或JSONArray,因为它们始终为null。

    try {
        if (jarray.equals(null)) {

        } else {

        }
    } catch (Exception e) {
        Toast.makeText(this, "Null Value", Toast.LENGTH_LONG).show();
    }

我不知道问题是什么。

public class Main extends Activity {
public ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
public static String urlPoblaciones = "http://192.168.1.168/Prueba/multitabla.php";
TextView txt1;
public static JSONObject jo;
public static JSONArray jarray;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txt1 = (TextView) findViewById(R.id.txt1);

    new CargarPoblaciones().execute();

    try {
        if (jarray.equals(null)) {

        } else {

        }
    } catch (Exception e) {
        Toast.makeText(this, "Null Value", Toast.LENGTH_LONG).show();
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

class CargarPoblaciones extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Main.this);
        pDialog.setMessage("Cargando poblaciones");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
        Log.d("Cargando: ", "Aun se esta cargando");
    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        Log.d("Cargando: ", "Vamos a cargar los productos");
        jo = jParser.makeHttpRequest(urlPoblaciones);
        Log.d("JSON", jo.toString());
        return "Hola";
    }

    @Override
    protected void onPostExecute(String string) {
        pDialog.dismiss();
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */

                txt1.setText(jo.toString() + "  " + jo.length());
                try {
                    //Creating an array from the JSON Object
                    jarray = jo.getJSONArray("Anunciante");
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }
}

}

1 个答案:

答案 0 :(得分:2)

只有在onPostExecute以及你在onCreate中调用它的方式后才会填充数组,但是尚未解析响应,所以你在那里得到null。

转移你的代码:

  try {
        if (jarray.equals(null)) {

        } else {

        }
    } catch (Exception e) {
        Toast.makeText(this, "Null Value", Toast.LENGTH_LONG).show();
    }

onPostExecute之后:

 jarray = jo.getJSONArray("Anunciante");  

您已经有一个进度条,您可以解雇。只需在onPostExecute之后调用dismiss,或在填充jarray后设置一个标志。