2014-10-01 91 views
1

在我的Android應用程序中,我有一個請求,直到昨天工作,現在它似乎忽略了onCreate。即使我將一個url傳遞給一個空的php文件,我也沒有錯誤,只是一個空的listview。JSONArray自定義請求停止工作

這裏是我的代碼:

public class CustomJsonRequest extends Request<JSONArray> { 

    Map<String, String> params; 
    customListener listener; 

    public CustomJsonRequest(int requestMethod, String url, Map<String, String> params, 
     customListener listener, Response.ErrorListener errorListener) { 

     super(requestMethod, url, errorListener); 
     this.params = params; 
     this.listener = listener; 
    } 
    @Override 
    protected void deliverResponse(JSONArray response) { 
     listener.onResponse(response); 

    } 

    @Override 
    protected Map<String, String> getParams() throws AuthFailureError { 
     return params; 
    } 


    @Override 
    protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) { 
     try { 
      String jsonString = new  
      String(response.data, 
      HttpHeaderParser.parseCharset(response.headers)); 
      return Response.success(new JSONArray(jsonString), 
       HttpHeaderParser.parseCacheHeaders(response)); 
     } catch (UnsupportedEncodingException e) { 
      return Response.error(new ParseError(e)); 
     } catch (JSONException je) { 
      return Response.error(new ParseError(je)); 
     } 
    } 
} 

在我的活動我有接口和聽衆

我在活動請求

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

    PDialog = new ProgressDialog(this); 
    // Showing progress dialog before making http request 
    PDialog.setMessage("..wait.."); 
    PDialog.show(); 

    SharedPreferences prefs = getSharedPreferences(MainActivity.SharedPrefName, 0); 
    nome= (prefs.getString("Name", "")); 
    genere= (prefs.getString("Sesso","")); 
    id= (prefs.getString("ID", "")); 
    gps = new GPSTracker(MapActivity.this); 
    link= (prefs.getString("LINK", "")); 
    age= (prefs.getString("AGE", "")); 
    foto= (prefs.getString("FOTO", "")); 
     // Get listview 
     lv = (ListView)findViewById(R.id.list);  
     adapter = new CustomListAdapter(this, productsList); 
     lv.setAdapter(adapter); 
CustomJsonRequest requi= new CustomJsonRequest(Request.Method.POST, 
       url_all_products_post, null, listener, error) 
     {protected Map<String, String> getParams() throws AuthFailureError { 
      Map<String, String> params = new HashMap<String, String>(); 
      params.put("id",id); 
      params.put("name", nome); 
      params.put("gender", genere); 
      params.put("lt", lt); 
      params.put("ln", ln); 
      params.put("age", age); 
      params.put("foto", foto); 
      params.put("link", link); 
      return params; 
     }; 
    }; 
    // Adding request to request queue 
    AppController.getInstance().addToRequestQueue(requi); 

} 
    public interface customListener{ 
    public void onResponse(JSONArray response); 
} 

ErrorListener error = new ErrorListener(){ 

    @Override 
    public void onErrorResponse(VolleyError error) { 
     // TODO Auto-generated method stub 

    }}; 

customListener listener= new customListener(){ 

    public void onResponse(JSONArray response) { 
     Log.d(TAG, response.toString()); 

     try { 
      // Parsing json array response 
      // loop through each json object 
      ... 
     } catch (JSONException e) { 
      e.printStackTrace(); 
      Toast.makeText(getApplicationContext(), 
         "Error: " + e.getMessage(), 
         Toast.LENGTH_LONG).show(); 
     } 
     // notifying list adapter about data changes 

     adapter.notifyDataSetChanged(); 

     hidepDialog(); 
    } 
}; 

爲什麼的onCreate「跳過」我的要求?

+0

我添加了onCreate。 – luigi988 2014-10-01 15:08:49

回答

0

我看不到任何理由,爲什麼下面不會像你所期望的那樣工作。

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

    PDialog = new ProgressDialog(this); 
    // Showing progress dialog before making http request 
    PDialog.setMessage("..wait.."); 
    PDialog.show(); 

    SharedPreferences prefs = getSharedPreferences(MainActivity.SharedPrefName, 0); 
    nome = (prefs.getString("Name", "")); 
    genere = (prefs.getString("Sesso", "")); 
    id = (prefs.getString("ID", "")); 
    gps = new GPSTracker(MapActivity.this); 
    link = (prefs.getString("LINK", "")); 
    age = (prefs.getString("AGE", "")); 
    foto = (prefs.getString("FOTO", "")); 
    // Get listview 
    lv = (ListView) findViewById(R.id.list); 
    adapter = new CustomListAdapter(this, productsList); 
    lv.setAdapter(adapter); 

    Map<String, String> params = new HashMap<>(); 
    params.put("id", id); 
    params.put("name", nome); 
    params.put("gender", genere); 
    params.put("lt", lt); 
    params.put("ln", ln); 
    params.put("age", age); 
    params.put("foto", foto); 
    params.put("link", link); 

    AppController.getInstance().addToRequestQueue(new CustomJsonRequest(Request.Method.POST, 
      url_all_products_post, params, new customListener() { 

     public void onResponse(JSONArray response) { 
      Log.d(TAG, response.toString()); 

      try { 
       // Parsing json array response 
       // loop through each json object 

      } catch (JSONException e) { 
       e.printStackTrace(); 
       Toast.makeText(getApplicationContext(), 
         "Error: " + e.getMessage(), 
         Toast.LENGTH_LONG).show(); 
      } 
      // notifying list adapter about data changes 

      adapter.notifyDataSetChanged(); 

      hidepDialog(); 
     } 
    }, new ErrorListener() { 

     @Override 
     public void onErrorResponse(VolleyError error) { 
      // TODO Auto-generated method stub 
      error.printStackTrace(); 
     } 
    })); 
}