获得" null"结果

时间:2018-05-21 06:31:09

标签: php android listview

我正在尝试使用php使用listView图像。我正在从表中检索数据并在我的'result''is '变量中获取null但是我的PHP代码工作正常。我不明白问题是什么问题在启动画面之后应用程序崩溃。这是我的listview

的代码
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mylist);

        listView=(ListView)findViewById(R.id.lsview);
        StrictMode.setThreadPolicy((new StrictMode.ThreadPolicy.Builder().permitNetwork().build()));
        collectData();
        CustomListView customListView=new CustomListView(this,name,details,images);
        listView.setAdapter(customListView);


    }

    private void collectData() {
        HttpURLConnection con = null;
        try {
            URL url = new URL(urladdress);
            con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");

            is = new BufferedInputStream(con.getInputStream());


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            con.disconnect();
        }
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line + "\n");

            }
            is.close();
            result = sb.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        try {


            JSONArray ja = new JSONArray(result);
            JSONObject jo = null;
            name = new String[ja.length()];
            details = new String[ja.length()];
            images = new String[ja.length()];

            for (int i = 0; i < ja.length(); i++) {
                jo = ja.getJSONObject(i);
                name[i] = jo.getString("name");
                details[i] = jo.getString("email");
                images[i] = jo.getString("Images");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

2 个答案:

答案 0 :(得分:0)

试试这段代码:

InvoiceGroupLine

答案 1 :(得分:0)

您的应用因此行而崩溃: -

CustomListView customListView = new CustomListView(this,name,details,images);

名称,详细信息和图像仅在网络呼叫完成后初始化,但您在此之前正在访问它们。

因此,您应该将此行移至collectData()方法,如下所示: -

onCreate(){
    listView=(ListView)findViewById(R.id.lsview);
    StrictMode.setThreadPolicy((new 
    StrictMode.ThreadPolicy.Builder().permitNetwork().build()));
    collectData();
}
private void collectData() {
    HttpURLConnection con = null;
        try {
            URL url = new URL(urladdress);
            con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");

            is = new BufferedInputStream(con.getInputStream());


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            con.disconnect();
        }

        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();

            while ((line = br.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }

        try {

            JSONArray ja = new JSONArray(result);
            JSONObject jo = null;
            name = new String[ja.length()];
            details = new String[ja.length()];
            images = new String[ja.length()];

            for (int i = 0; i < ja.length(); i++) {
                jo = ja.getJSONObject(i);
                name[i] = jo.getString("name");
                details[i] = jo.getString("email");
                images[i] = jo.getString("Images");
            }

        CustomListView customListView=new 
        CustomListView(this,name,details,images);
        listView.setAdapter(customListView);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }