在我的Android应用程序中,我有一个请求,直到昨天工作,现在它似乎被onCreate忽略了。即使我将一个url传递给一个空的php文件,我也没有错误,只是一个空的listview。
这是我的代码:
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 :(得分: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();
}
}));
}