Listview使用Asynctask没有得到服务器的响应

时间:2014-03-24 07:23:36

标签: java android listview android-listview android-asynctask

我想将postParamName中的Cus_id发送到Web服务器。 根据cus_id,我想从服务器获取数据并将其放入listview。

我的代码中没有错误...但代码仍然无法从服务器获取数据..

Plz看看我的代码......自从过去两天以来我一直在研究这段代码。但我无法找到错误

Point1.java

            public class Points1 extends ListActivity implements FetchDataListener {
SessionManager session;
TextView tvCusPoints1, tvCusPoints2, tvcusName;
TextView bus_name;
TextView cus_points;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.points);
    initView();
              }
            private void initView() {
    session = new SessionManager(getApplicationContext());
    // get user data from session
    HashMap<String, String> user = session.getUserDetails();
    // ID
    String cus_id = user.get(SessionManager.KEY_ID);
    ArrayList<NameValuePair> postParamName = new ArrayList<NameValuePair>();
    postParamName.add(new BasicNameValuePair("cus_id", cus_id));

    String url = "http://10.0.2.2/android_api_main/business_points.php";
    FetchDataTask task = new FetchDataTask(this);
    task.execute(url);
}
      @Override
public void onFetchComplete(List<Application> data) {
    // dismiss the progress dialog

    // create new adapter
    ApplicationAdapter adapter = new ApplicationAdapter(this, data);
    // set the adapter to list
    setListAdapter(adapter);
}
      @Override
public void onFetchFailure(String msg) {
    // dismiss the progress dialog

}
        }

Application.java

      public class Application 
       {

   private String bus_name;
   private String cus_points;

public String getbus_name() {
    return bus_name;
}

public void setbus_name(String bus_name) {
    this.bus_name = bus_name;
}

public String getcus_points() {
    return cus_points;
}

public void setcus_points(String cus_points) {
    this.cus_points = cus_points;
}
    }

ApplicationAdapter.java

       public class ApplicationAdapter extends ArrayAdapter<Application> {
    private List<Application> items;

 public ApplicationAdapter(Context context, List<Application> items) {
    super(context, R.layout.point_list_item, items);
    this.items = items;
}

@Override
public int getCount() {
    return items.size();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater li = LayoutInflater.from(getContext());
        v = li.inflate(R.layout.point_list_item, null);
    }
    Application app = items.get(position);
    if (app != null) {
        TextView titleText = (TextView) v.findViewById(R.id.item_bname1);
        TextView dlText = (TextView) v.findViewById(R.id.item_bpoint1);

        if (titleText != null)
            titleText.setText(app.getbus_name());
        if (dlText != null)
            dlText.setText(app.getcus_points());
    }

    return v;
}
       }

FetchDataTask.java

      public class FetchDataTask extends AsyncTask<String, Void, String> {
private final FetchDataListener listener;
private String msg;
String cus_id, responseString, success, bus_name, cus_points;
SessionManager session;

public FetchDataTask(FetchDataListener listener) {
    this.listener = listener;
}

@Override
protected String doInBackground(String... params) {
    if (params == null)
        return null;

    // get url from params
    String url = params[0];
    try {

        // create http connection
        HttpClient client = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);
        // connect
        HttpResponse response = client.execute(httpget);
        // get response
        HttpEntity entity = response.getEntity();
        responseString = EntityUtils.toString(entity);
        // get response content and convert it to json string

    } catch (IOException e) {
        msg = "No Network Connection";
    }
    return responseString;
}

@Override
protected void onPostExecute(String sJson) {
    try {
        JSONObject json = new JSONObject(responseString);
        JSONArray jArray = json.getJSONArray("customer");
        List<Application> apps = new ArrayList<Application>();
        for (int i = 0; i < jArray.length(); i++) {
            JSONObject json_data = jArray.getJSONObject(i);
            bus_name = json_data.getString("bus_name");
            cus_points = json_data.getString("cus_points");
            success = json_data.getString("success");
            Application app = new Application();
            app.setbus_name(json.getString("bus_name"));
            app.setcus_points(json.getString("cus_points"));
            // add the app to apps
            apps.add(app);
        }
        if (listener != null)
            listener.onFetchComplete(apps);
    } catch (JSONException e) {
        msg = "Invalid response";
        if (listener != null)
            listener.onFetchFailure(msg);
        return;
    }
}
              }

FetchDataListener.java

     public interface FetchDataListener {
public void onFetchComplete(List<Application> data);

public void onFetchFailure(String msg);
     }

1 个答案:

答案 0 :(得分:0)

您的FetchDataTask构造函数接受FetchDataTaskListener作为参数

public FetchDataTask(FetchDataListener listener) {
    this.listener = listener;
}

但您已使用活动的上下文

初始化它
FetchDataTask task = new FetchDataTask(this);

请你检查一下。

你应该正确设置监听器,就像这样

this.mListener = (FetchDataListener) activity