I am using volley library to fetch json array via POST request. when I use get method am able to fetch the desired values but when I use POST method am unable to fetch any value instead am getting this error
value of type org.json.jsonobject cannot be converted to jsonarray
Here is my code
public class xyz extends Activity {
private static final String url = "http://example.com/q.php";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_main);
listView = (ListView) findViewById(R.id.listview);
adapter = new CustomListAdapter(this, movieList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// changing action bar color
Response.ErrorListener errorListener = new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError errorResponce)
{
VolleyLog.d( "Error: " + errorResponce.getMessage());
hidePDialog();
}
};
Response.Listener<JSONArray > jsonArrayListener = new Response.Listener<JSONArray>()
{
#Override
public void onResponse(JSONArray response) {
// TODO Auto-generated method stub
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("ttl"));
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
};
Map<String, String> mParam = new HashMap<String, String>();
mParam.put("q", "ram");
PostJsonArrayRequest req = new PostJsonArrayRequest(Request.Method.POST, url , null, jsonArrayListener, errorListener,mParam);
// Creating volley request obj
// Adding request to request queue
AppController.getInstance().addToRequestQueue(req);
// Adding request to request queue
}
#Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
public class PostJsonArrayRequest extends JsonRequest<JSONArray> {
private Map<String,String> mParam;
private Response.Listener<JSONArray> mListener;
public PostJsonArrayRequest(int method, String url, String requestBody,
Response.Listener<JSONArray> listener, Response.ErrorListener errorListener,Map param) {
super(method, url, requestBody, listener, errorListener);
mListener=listener;
mParam=param;
}
#Override
protected Map<String, String> getParams() throws AuthFailureError {
return mParam;
}
#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 deliverResponse(JSONArray response) {
mListener.onResponse(response);
}
}
}
please guide me where am making the mistake. am i passing the params correctly?
Related
I'm sending the holder view to volley method in ArrayAdapter like
final ViewHolder finalHolder = holder;
holder.rlLike.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
userLikes(finalHolder);
}
});
private void userLikes(final ViewHolder finalHolder){
StringRequest request = new StringRequest(Request.Method.POST, user_likes_url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
if(response != null){
Log.e("LikeResponse", response);
try {
JSONObject jsonObject = new JSONObject(response);
//toastShort(activity, user_likes_obj.getString("data"));
if(jsonObject.getString("status").equals("success")){
finalHolder.userLikeImage.setImageResource(R.drawable.icon_pop_like);
finalHolder.tvLike.setText("Unlike");
}else {
}
} catch (JSONException e) {
e.getLocalizedMessage();
e.printStackTrace();
}
}else{
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("error", error.toString());
}
}){
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
Log.e("action", user_likes);
params.put("action",user_likes);
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Content-Type","application/x-www-form-urlencoded");
return params;
}
};
RequestQueue queue = Volley.newRequestQueue(context);
queue.add(request);
queue.getCache().remove(user_likes_url);
}
After getting the successful response from the server I'm changing the ImageView and TextView like
finalHolder.userLikeImage.setImageResource(R.drawable.icon_pop_like);
finalHolder.tvLike.setText("Unlike");
But I want to send the holder view to asynchronus task and bassed on the server response I want to change ImageView and TextView as above
My AsyncTask task code is
private void userLikes(String likeStatus){
class carRegAsynTask extends AsyncTask<String, Void, String>{
//ProgressDialog dialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected String doInBackground(String... params) {
String likeStatusParam = params[0];
BasicNameValuePair likeStatus = new BasicNameValuePair("action", likeStatusParam);
List<NameValuePair> listValuePairs = new ArrayList<NameValuePair>();
listValuePairs.add(likeStatus);
Log.e("listValuePairs", listValuePairs.toString());
String Url = user_likes_url;
Log.e("Url", Url);
String response = POST_METHOD(listValuePairs, Url);
return response;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (result != null) {
Log.e("LikeResult2", result);
try {
JSONObject jsonObject = new JSONObject(result);
if(jsonObject.getString("status").equals("success")){
userLikeImage.setImageResource(R.drawable.icon_pop_un_like);
tvLike.setText("Like");
}else {
}
} catch (JSONException e) {
e.getLocalizedMessage();
e.printStackTrace();
}
}
}
}
new carRegAsynTask().execute(likeStatus);
}
So please help me to send the holder view to Asynchronus task like in the volley method I discussed above, and don't forget it is an ArrayAdapter
Although this is not a good practice to hold strong references to views from asynchronous tasks, you can pass whatever you need to your carRegAsynTask. Just create a constructor and pass all the needed data as parameters.
UPDATE
Example:
class carRegAsynTask extends AsyncTask<String, Void, String>{
private final ViewHolder viewHolder;
public carRegAsynTask(ViewHolder viewHolder) {
this.viewHolder = viewHolder;
}
// omitted for brevity...
// Use your viewHolder in onPreExecute or onPostExecute
}
// Creating async task with your viewHolder:
new carRegAsynTask(viewHolder).execute(likeStatus);
I don't know whether it follow able or not, but i achieved it in this way
private void userLikes(final ViewHolder finalHolder) {
class userLikesAsynTask extends AsyncTask<String, Void, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected String doInBackground(String... params) {
BasicNameValuePair userid = new BasicNameValuePair("action", user_likes);
List<NameValuePair> listValuePairs = new ArrayList<NameValuePair>();
listValuePairs.add(userid);
Log.e("listValuePairs", listValuePairs.toString());
String userLikesUrl = user_likes_url;
Log.e("userLikesUrl", userLikesUrl);
String response = POST_METHOD(listValuePairs, userLikesUrl);
return response;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (result != null) {
Log.e("userLikesUrlResult", result);
try {
JSONObject jsonObject = new JSONObject(result);
if(jsonObject.has("status")){
if(jsonObject.getString("status").equals("success")){
finalHolder.userLikeImage.setImageResource(R.drawable.icon_pop_like);
finalHolder.tvLike.setText("Unlike");
}else {
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
new userLikesAsynTask().execute();
}
While I was testing the "volley" library, a doubt arose me regarding the method "post".
The thing is that I have been working with JsonObject until now, for that I used the following code and it works. In this method I send and JsonObject and receive another JsonObjet, I had no problem with it.
Now my doubt is, How can I do to send a JsonObject and receive an ArrayObject?
I am really lost with this, I will really appreciate your help, thanks in advance
public void testPost(String url, final String tag, JSONObject obj) {
//final ProgressDialog pDialog = new ProgressDialog(context);
//pDialog.setMessage("Loading...");
//pDialog.show();
JsonObjectRequest jsonObjReqpost = new JsonObjectRequest(Request.Method.POST,
url, obj,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
//test
Log.d("SendRequestJsonPost", response.toString());
//pDialog.hide();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
if (error.networkResponse != null && error.networkResponse.data != null) {
error = new VolleyError(new String(error.networkResponse.data));
}
String fail = handlingErrors.resolverErrors(error.toString());
//Log.d("SendRequestJsonPost", fail);
//pDialog.hide();
}
}) {
//header
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("charset", "utf-8");
return headers;
}
};
jsonObjReqpost.setTag(tag);
Singleton.getInstance(context).addToRequestQueue(jsonObjReqpost);
}
You can use the following method to get JsonArray as response.
public JsonArrayRequest(int method, String url, JSONObject jsonRequest,
Listener<JSONArray> listener, ErrorListener errorListener)
{
super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(),
listener, errorListener);
}
Please refer this answer for more details.
public class nameOfClass extends AsyncTask {
String response;
//This process is work in Background
#Override
protected String doInBackground(String... params) {
SocketConnection connection = new SocketConnection();
String token;
//Tocken name that is send to server
token = "TockenName|" + "|";
try {
response = connection.EstablishConnection(token);
String message = "Sorry Fail to connect";
if (response.equals(message)) {
onPostExecute(message);
}
}
catch (Exception e)
{
e.printStackTrace();
}
return response;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
// In this method you can open loading msg
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
//Json object
JSONArray array = new JSONArray(result);
for (int i = 0; i < array.length(); i++) {
//Hear traverse the loop and get the data
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
I am using volley.jar to fetch JSON array from remote server. Am able to fetch via GET request.Please help me with POST request
I am following a tutorial from androidhive.
Here is my working code.
public class fbuk extends Activity {
// Movies json url
private static final String url = "http://example.com/xyz.php?q=ram";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_main);
listView = (ListView) findViewById(R.id.listview);
adapter = new CustomListAdapter(this, movieList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("ttl"));
movie.setThumbnailUrl(obj.getString("img"));
movie.setRating(obj.getString("stts"));
movie.setYear(obj.getString("relz"));
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
}
#Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
}
How to send params
public void Post(String url, final Map<String, String> map) {
showProgress();
RequestQueue queue = Volley.newRequestQueue(_serverctx);
StringRequest request = new StringRequest(Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
if (response!=null && response.length()>0) {
try {
_response.result(new JSONObject(response)); // Pass data to your parsing method(in my case i made a interface)
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
System.out.println("error");
hideProgres();
}
}){
#Override
protected Map<String, String> getParams() {
return map;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Content-Type","application/x-www-form-urlencoded");
return params;
}
};
request.setShouldCache(false);
request.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 1, 1.0f));
queue.add(request);
}
Call from Activity or fragment
Put the above method in a seprate class and give a call like
Map<String,String> params = new HashMap<String, String>();
params.put("email",email);
params.put("password",password);
_req = new NetworkRequests(this, ctx);
_req.Post(ShoqahContants.Login_URL, params);
and your are Done :)
This worked for me.
pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
url="http://www.example.com/xyz.php";
Volley.newRequestQueue(your_activity.this).add(
new CustomJsonRequest(Request.Method.POST, url, null,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
GC gc = new GC();
gc.setTitl(obj.getString("ttl"));
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Error: " + error.getMessage());
hidePDialog();
}
}) {
#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
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
public class CustomJsonRequest extends Request {
Map<String, String> params;
private Response.Listener listener;
public CustomJsonRequest(int requestMethod, String url, Map<String, String> params,
Response.Listener responseListener, Response.ErrorListener errorListener) {
super(requestMethod, url, errorListener);
params=new HashMap<String, String>();
params.put("key", value);
this.params = params;
this.listener = responseListener;
}
#Override
protected void deliverResponse(Object response) {
listener.onResponse(response);
}
#Override
public Map<String, String> getParams() throws AuthFailureError {
return params;
}
#Override
protected Response parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
p.s:the json must be in the below format
[{"titl":"The Great gatsby"},{"titl":"Wolf Of Wall Street"}]
I am having this class where am fetching json array with POST method after referring several google searches and stackoverflow question but my code gives an weird error Cannot Resolve Symbol POST
here is my activity
public class fbuk extends Activity {
// Movies json url
private static final String url = "http://example.com/xyz.php";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_main);
listView = (ListView) findViewById(R.id.listview);
adapter = new CustomListAdapter(this, movieList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// changing action bar color
// Creating volley request obj
JsonArrayRequest movieReq = new PostJsonArrayRequest(Method.POST, url, null,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("ttl"));
movie.setThumbnailUrl(obj.getString("img"));
movie.setRating(obj.getString("stts"));
movie.setYear(obj.getString("rel"));
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
}
#Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
}
public class PostJsonArrayRequest extends JsonRequest<JSONArray> {
/**
* Creates a new request.
* #param url URL to fetch the JSON from
* #param listener Listener to receive the JSON response
* #param errorListener Error listener, or null to ignore errors.
*/
public PostJsonArrayRequest(String url, Response.Listener<JSONArray> listener, Response.ErrorListener errorListener) {
super(Method.POST, url, null, listener, errorListener);
}
#Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> params = new HashMap<String, String>();
params.put("q", "ram");
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));
}
}
}
I am following a tutorial from androidhive
Please suggest where am making the mistake
I have done like this (see sample code), make sure that import com.android.volley.Request; must be imported in your activity class otherwise Request.Method.POST will not work and Unable to Resolve will appear
String url = "http://example.com/xyz.php";
ErrorListener errorListener = new ErrorListener() {
#Override
public void onErrorResponse(VolleyError errorResponce)
{
}
};
Listener<JSONArray > jsonArrayListener = new Listener<JSONArray>()
{
#Override
public void onResponse(JSONArray response) {
// TODO Auto-generated method stub
}
};
PostJsonArrayRequest req = new PostJsonArrayRequest(Request.Method.POST, url , null, jsonArrayListener, errorListener);
And use this class for
public class PostJsonArrayRequest extends JsonRequest<JSONArray>
{
public PostJsonArrayRequest(int method, String url, String requestBody,
Listener<JSONArray> listener, ErrorListener errorListener)
{
super(method, url, requestBody, listener, errorListener);
}
#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));
}
}
}
Check your import for Method. It should be
import com.android.volley.Request.Method
May be you have imported as a
import java.lang.reflect.Method;
Use
JsonArrayRequest movieReq = new PostJsonArrayRequest(Request.Method.POST,url,
new Response.Listener<JSONArray>() {
and make sure to import,
import com.android.volley.Request.Method;
hope it will work.
In my android app I have a request which until yesterday was working, now it seems ignored by the onCreate. Even if I pass a url to an empty php file, I have no errors, just an empty listview.
Here's my code:
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));
}
}
}
In my activity I have the interface and the listeners
My request in the activity is
#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();
}
};
Why the onCreate "skips" my request?
I can't see any reason why the below wouldn't work as you would expect.
#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();
}
}));
}