Android http request synchron with Volley - android

I write code to make a request http to server:
public void reqCategory() {
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://myaddress/getdata.php";
StringRequest postRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
//Json Parse
try {
JSONArray jsonArray = new JSONArray(response);
catList.clear();
for (int i=0; i<jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
catList.add(jsonObject.getString("categoria"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("Error -> ", error.toString());
}
}) {
#Override
protected Map<String, String> getParams() { //nessun parametro
Map<String, String> params = new HashMap<String, String>();
//no param at moment
return params;
}
};
queue.add(postRequest);
}
I declare:
List<String> catList = new ArrayList<String>();
in my onCreate I have:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comanda);
reqCategory();
for (String s : catList) {
Log.d("Categoria -> ", s);
}
}
This should be fill the List catList with values from reqCategory(), but the for loop doesnt show the values, because the request is asyncron.
How I can make a syncron request so I have the values in catList after the reqCategory() call and I can see the values from loop for.
Thank you.

try some minor edit in your code
for (int i=0; i<jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String Temp = new
catList.add(new String(jsonObject.getString("categoria")));
}

You cannot have networking blocking UI thread on Android. All networking must be on separate thread, therefore you need to refactor your code to be like, i.e.:
onCreate() {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comanda);
reqCategory();
}
where reqCategory() must spawn i.e. AsyncTask for the networking. Once AsyncTask is done you can executre your loop in its onPostExecute() (or in odInBackground() if you do not need to touch UI widgets).

Related

How to send JSON Array of objects using Volley in android

I want to send some bulk data to my php server so I constructed JSON Array. But how to send it using volley in Android. Could you anybody help. I already tried many ways but didnt work.
Below is my code for the dataset
JSONArray jsData = new JSONArray();
JSONObject others = new JSONObject();
while(crsrallansr.isAfterLast() == false) {
JSONObject Inner = new JSONObject();
try {
Inner.put("qid",crsrallansr.getString(crsrallansr.getColumnIndex("qid")));
Inner.put("qstn",crsrallansr.getString(crsrallansr.getColumnIndex("qid")));
Inner.put("result",crsrallansr.getString(crsrallansr.getColumnIndex("qid")));
} catch (JSONException e) {
e.printStackTrace();
}
jsData.put(Inner);
crsrallansr.moveToNext();
xx++;
}
Fixed the problem using StringRequest like :
reqPostanswers = new StringRequest(Request.Method.POST, url,new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.i("posting info :",response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//Log.i("posting error :",error.toString());
}
}){
#Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("user", thisuser);
params.put("answers",jsData.toString());
params.put("lickey","1761");
return params;
}
};
answerpostQueue = Volley.newRequestQueue(getApplicationContext());
answerpostQueue.add(reqPostanswers);
At the server side ( php ); the code is as follows :
$answers=json_decode($_POST['answers']);
foreach ($answers as $answer) {
$answer=json_encode($answer);
echo $answer;
$answer=json_decode($answer);
$uname=$_POST['user'];
$qid=$answer->qid;
$result=$answer->result;
$qstn=$answer->qstn;

Store JSONArrayRequest response in Android Studio

Good morning! I've been a lot of days searching for an answer similar to this but I couldn't find it, so here I am.
In Android Studio, I've did a function which has a jsonArrayRequest request, which looks like this:
private String requestCoursesInfo() {
RequestQueue requestQueue = Volley.newRequestQueue(this.context);
JsonArrayRequest jsonArrayRequest= new JsonArrayRequest(
Request.Method.GET,
COURSES_URL,
null,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray coursesJsonArray) {
String name;
ArrayList<HashMap> coursesBasicInfoList = new ArrayList<>();
try {
for (int i=0; i < coursesJsonArray.length(); i++){
JSONObject courseInfo = coursesJsonArray.getJSONObject(i);
name = courseInfo.getString("name");
//ArrayList to group all the info
HashMap<String, String> currentCourseInfoList = new HashMap<>();
currentCourseInfoList.put("name", name);
coursesBasicInfoList.add(currentCourseInfoList);
}
} catch (JSONException e) {
//Do something with error
}
}
},
new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError error){
// Do something when error occurred
}
}
){
#Override
public Map<String, String> getHeaders() {
Map<String, String> params = new HashMap<>();
params.put("Content-Type", "application/json");
return params;
}
};
// Add JsonObjectRequest to the RequestQueue
requestQueue.add(jsonArrayRequest);
return "courses";
}
The thing is, as you can see, once I have the response I build a HashMap with the key name and value of the name from the response and I put it all in coursesBasicInfoList ArrayList.
The thing is, this variable (coursesBasicInfoList) is never accessible from outside the request and what I want is to be able to put this arraylist in the "return" that you can see at the end of the function.
I know the request is asynchronous but I suppose it has to be some way to store this data from the response to use it in other methods, isn't it?
Thank you!
You can use LiveData to solve synchronization problem in asynchronous call like volley.
private MutableLiveData<ArrayList<HashMap>> requestCoursesInfo() {
MutableLiveData<ArrayList<HashMap>> mutableCoursesBasicInfoList = new MutableLiveData<>();
RequestQueue requestQueue = Volley.newRequestQueue(this.context);
JsonArrayRequest jsonArrayRequest= new JsonArrayRequest(
Request.Method.GET,
COURSES_URL,
null,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray coursesJsonArray) {
String name;
ArrayList<HashMap> coursesBasicInfoList = new ArrayList<>();
try {
for (int i=0; i < coursesJsonArray.length(); i++){
JSONObject courseInfo = coursesJsonArray.getJSONObject(i);
name = courseInfo.getString("name");
//ArrayList to group all the info
HashMap<String, String> currentCourseInfoList = new HashMap<>();
currentCourseInfoList.put("name", name);
coursesBasicInfoList.add(currentCourseInfoList);
}
} catch (JSONException e) {
//Do something with error
}
//post your result in LiveData
mutableCoursesBasicInfoList.postValue(coursesBasicInfoList);
}
},
new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError error){
// Do something when error occurred
}
}
){
#Override
public Map<String, String> getHeaders() {
Map<String, String> params = new HashMap<>();
params.put("Content-Type", "application/json");
return params;
}
};
// Add JsonObjectRequest to the RequestQueue
requestQueue.add(jsonArrayRequest);
//Return MutableLiveData<ArrayList<HashMap>>
return mutableCoursesBasicInfoList;
}
And the observe the LiveData from your Activity/Fragment like below:
requestCoursesInfo().observe(this, new Observer<ArrayList<HashMap>>() {
#Override
public void onChanged(ArrayList<HashMap> hashMaps) {
//Do your operation here with ArrayList<HashMap>
}
});

Android volley does not calling getParams in POST request

friends. I have a problem with my volley POST request with parameters. I need fetch the data from my remote MySQL DB and display it in recycler view. For this, I have using volley network request. Without parameters, it loads all the data from my DB. But when I add getParams() method it returns a null array even though corresponding DB entries are available according to the passing parameters. Here is my code..
public void setDonorsList(final VolleyCallBack volleyCallBack) {
if (Utility.isNetworkEnabled) {
final ArrayList<Donor> listOfDonors = new ArrayList<>();
final ProgressDialog progressDialog = new ProgressDialog(FindDonorResult.this);
progressDialog.setMessage("Loading Donors List. Please wait");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, Constants.GET_DONORS_URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
progressDialog.dismiss();
try {
listOfDonors.clear();
JSONArray jsonArray = new JSONArray(response);
int count = 0;
while (count < 10) {
JSONObject jsonObject = jsonArray.getJSONObject(count);
Donor donor = new Donor(
jsonObject.getString("fName"), jsonObject.getString("sName"), jsonObject.getString("emailid"),
jsonObject.getString("pass"), jsonObject.getString("mobile"), jsonObject.getString("blood"),
jsonObject.getString("age"), jsonObject.getString("gender"), jsonObject.getString("country"),
jsonObject.getString("location"), jsonObject.getString("latitude"), jsonObject.getString("longitude"),
jsonObject.getString("picname"), jsonObject.getString("pic")
);
int roundedDistance = (int) distance(Double.parseDouble(donor.getLatitude()),
Double.parseDouble(latitude), Double.parseDouble(donor.getLongitude()),
Double.parseDouble(longitude));
donor.setDistance(roundedDistance);
listOfDonors.add(donor);
count++;
}
Log.e("listOfDonors", String.valueOf(listOfDonors.size()));
volleyCallBack.onSuccess(listOfDonors);
} catch (JSONException e) {
e.getMessage();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.getMessage();
}
}
){
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("bloodGroup", "A+");
return params;
}
};
NetworkRequestSingleTon.getOurInstance(FindDonorResult.this).addToRequestQue(stringRequest);
} else {
Toast.makeText(FindDonorResult.this, "No active internet connection.", Toast.LENGTH_SHORT).show();
}
}
try using this
JSONObject jObj = new JSONObject(response)
JSONArray area = jObj.getJSONArray("");
maybe this will help
Finally, I figured it. The problem was in setDonorsList() method. I have hard coded the condition while(count < 10). This produces a JSON exception called "index out of range" since my DB doesn't have 10 entries. I changed the value like while(count < jsonArray.length()). Now everything is perfect. Guys thank you so much for your time and concern.

Volley not sending the params while using JsonArrayRequest

The Android code is not sending the parameters when using JsonArrayRequest.
If I use StringRequest instead of JsonArrayRequest how do I convert the response from String to JSONObject to display it in a RecyclerView?
Intent intent = getIntent();
final String userID = intent.getExtras().getString("userID");
recyclerView = (RecyclerView)findViewById(R.id.display_expenses_recycler_view_id);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, expensesDisplay_url, (String) null,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
int count = 0;
Log.i("Response", String.valueOf(response));
while (count < response.length()) {
try {
JSONObject jsonObject = response.getJSONObject(count);
ExpensesDetails expensesDetails = new ExpensesDetails(jsonObject.getString("Expenses"),
jsonObject.getString("Description"), jsonObject.getString("datetime"));
arrayList.add(expensesDetails);
count++;
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(DisplayExpenses.this, "Error", Toast.LENGTH_LONG).show();
error.printStackTrace();
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("userID", userID);
return params;
}
};
Log.i("Array", String.valueOf(arrayList));
MySingleton.getMyInstance(DisplayExpenses.this).addToRequestQueue(jsonArrayRequest);
adapter = new RecyclerAdapterExpenses(arrayList);
recyclerView.setAdapter(adapter);View.setAdapter(adapter);
This is the result sent from the server:
[
{
"Expenses":"0",
"Description":"car",
"datetime":"2016-10-25 21:10:57"
},
{
"Expenses":"2000",
"Description":"Car",
"datetime":"2016-10-25 21:46:05"
},
{
"Expenses":"5000",
"Description":"House payment",
"datetime":"2016-10-25 21:47:11"
},
{
"Expenses":"200",
"Description":"",
"datetime":"2016-10-26 20:51:42"
},
{
"Expenses":"500",
"Description":"",
"datetime":"2016-10-26 23:55:21"
}
]
Are you sure the user ID isn't being given? You have an empty RecyclerView until the Volley request finishes. And your Log.i statement happens before Volley completes, so you'll see an empty list...
You need an adapter.notifyDataSetChanged() within the onResponse call after the while loop to tell the adapter to display the data you added.
Not too sure what View is here... but you likely don't need the same adapter on two views.
recyclerView.setAdapter(adapter);View.setAdapter(adapter);
if i tried to use StringRequest instead of JsonArrayRequest how will i convert the string response from String to JSONObject to display them in a RecyclerView
You have a String response from onResponse, so use the constructor for JSONArray that takes a String.
JSONArray array = new JSONArray(response);
Note: that requires a try-catch.

Volley POST REQUEST error 500

JSON:
{
"isRegistrationSuccess":"true"
}
This what my backend should provide while user successfully register in system. I am sending Name, Email and Password as parameters. I am getting 500 error.
/Volley: [188] BasicNetwork.performRequest: Unexpected response code 500 for http://100.100.202.200/mobile/register?name=admin&email=admin#nomail.com&password=admin123
Although, I can see the user information in my backend. Here is my code:
RequestQueue queue = Volley.newRequestQueue(this);
String url_to_parse = getLink(name,email,password).trim();
StringRequest stringReq = new StringRequest(Request.Method.POST, url_to_parse, new Response.Listener<String>() {
#Override
public void onResponse(String response){
try{
Log.d("Response",response);
JSONArray obj = new JSONArray();
boolean isLoginSuccess = Boolean.parseBoolean(obj.getString(0));
if(isLoginSuccess){
onSignupSuccess();
}else{
onSignupFailed();
}
}catch (JSONException e){
e.printStackTrace();
onSignupFailed();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
onSignupFailed();
Log.e("Error",String.valueOf(error.getMessage()));
}
});
queue.add(stringReq);
I am not sure what is wrong I am doing here? How can I solve it?
POST data is given in a protected Map getParams () and not the URL:
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("parametr1","value1");
params.put("parametr2","value2");
params.put("parametr3","value3");
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;
}
Fix your url and use JsonObjectRequest
You want to parse a array into a boolean, you have to loop through the array like this:
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray= jsonObject.getJSONArray("example");
if (jsonArray.length() != 0) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jo = jsonArray.getJSONObject(i);
boolean isLoginSuccess = Boolean.parseBoolean(jo.getString("exampleString"));
}
}

Categories

Resources