0

我的申请有问题。我正在尝试在 android 的自定义类中从我的数据库中传递 json 数据,然后将其显示在列表中。当我运行我的应用程序时,没有任何反应,没有错误,没有显示列表。如果有人可以帮助我将非常感激!:)

所有网络的东西都是异步完成的,我试图返回一个对象数组,所以我怀疑这可能是问题在这里,或者当我将字符串从 httphandler 类转换为 JSONArray 时。

这是我的主要活动

package com.example.test1;



import java.util.ArrayList; 
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class clubpage extends Activity {

class Programme {

    public String name;
    public String event;
    public String price;

}

String clubphp = "http://10.0.2.2/corkgaa/Nemo.php";
String progString;
ArrayList<Programme> Programmedata = new ArrayList<Programme>();
ListView clublistview = (ListView)findViewById(R.id.listview1);

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.clubpage);

    new Dbhandler().execute(clubphp);   

    ArrayAdapter<Programme> adapter = new ArrayAdapter<Programme>(this,
            android.R.layout.simple_list_item_1, Programmedata);

    clublistview.setAdapter(adapter);


}   

public class Dbhandler extends AsyncTask<String, Void, ArrayList<Programme>> {

    protected ArrayList<Programme> doInBackground(String... arg0) {

ArrayList<Programme> arraydata = new ArrayList<Programme>();        
progString = httphandler.HttpGetExec(clubphp);

try{ 

    JSONArray jArray = new JSONArray(progString);
    JSONObject json_data=null; 

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

        json_data = jArray.getJSONObject(i); 
        Programme Progresult = new Programme();

        Progresult.name = json_data.getString("Name");
        Progresult.event = json_data.getString("Event");
        Progresult.price = json_data.getString("Price");

        arraydata.add(Progresult);

        }
    }
    catch(JSONException e1){ 

        }
    catch (ParseException e1) { 
        e1.printStackTrace();
        }
return arraydata;
    }

    @Override
    protected void onPostExecute(ArrayList<Programme> result) {

    Programmedata = result;
    }

}




@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
}

}

httphandler 类在这里:

package com.example.test1;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import android.util.Log;

public class httphandler {

//Main Dev setup

public static String HttpGetExec (String URI) {


    // TODO Auto-generated method stub
                String result = "no response"; 
                InputStream is = null; 
                StringBuilder sb = null;


                //http post 
                try{ 
                    HttpClient httpclient = new DefaultHttpClient(); 
                    HttpPost httppost = new HttpPost("http://10.0.2.2/corkgaa/Nemo.php"); 
                    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()); 
                    }

                //convert response to string 
                try{ 
                    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 converting result "+e.toString()); 
                    }

                return result;

                //aa=new ArrayAdapter<String>(clubpage.this, R.layout.listrow, R.id.title, result);
                //ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.listrow, R.id.title, result);
                //listview.setAdapter(aa);

}

}

4

1 回答 1

1

将此行移至您的onCreate后面setContentView

clublistview = (ListView)findViewById(R.id.listview1);

并将以下行移至异步任务中的 onPostExecute :

ArrayAdapter<Programme> adapter = new ArrayAdapter<Programme>(this,
            android.R.layout.simple_list_item_1, Programmedata);

clublistview.setAdapter(adapter);

在执行异步任务时,请考虑显示某种进度指示器。

于 2013-03-11T14:43:53.890 回答