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.
Related
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;
I am trying to pass parameters to a POST request by using Volley in an Android Studio Project.
In fact i want to send a nickname through a POST request.
By this way my php file will send me back a json array that i will parse.
I tested my parsing by wrinting hard this nickname in my php file => it worked and i get back a json with the corrects values.
Now i want to send this nickname by adding him in the parameters of my POST request.
I tried to use the GetParams method but it doesnt work.
This is my code
private void loadJsonArray() {
final String pseudo = "test";
JsonArrayRequest jsArrayRequest = new JsonArrayRequest
(Request.Method.POST, array_url, null, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray responseArray) {
pDialog.dismiss();
try {
StringBuilder textViewData = new StringBuilder();
//Parse the JSON response array by iterating over it
for (int i = 0; i < responseArray.length(); i++) {
JSONObject response = responseArray.getJSONObject(i);
Integer centreInteretId = response.getInt("Id");
String libelle = response.getString("Libelle");
Integer IsSelect = response.getInt("IsSelect");
//Create String out of the Parsed JSON
textViewData.append("CentreInteret Id: ")
.append(centreInteretId.toString()).append(NEW_LINE);
textViewData.append("Libelle: ").append(libelle).append(NEW_LINE);
textViewData.append("IsSelect: ").append(IsSelect).append(NEW_LINE);
//Populate textView with the response
mTxtDisplay.setText(textViewData.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
pDialog.dismiss();
//Display error message whenever an error occurs
Toast.makeText(getApplicationContext(),
error.getMessage() + " lol", Toast.LENGTH_SHORT).show();
}
}){
#Override
protected Map<String, String> getParams() throws AuthFailureError{
Toast.makeText(getApplicationContext()," les params", Toast.LENGTH_SHORT).show();
Map<String,String> map = new HashMap<String,String>();
map.put("pseudo",pseudo);
//map.put("oldPassword",oldPassword);
return map;
}
};
// Access the RequestQueue through your singleton class.
MySingleton.getInstance(this).addToRequestQueue(jsArrayRequest);
}
As you can see in my code im trying to send the value "test" through a POST request
Do you have an idea of where is the problem ?
Thanks in advance
if request need Content-Type in Header you need override headers of your Request
By This Method :
#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;
}
if don't work pleas tell me to search for another solution .
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).
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.
I want to make 7 JSON Requests in one single activity. Is there any way to do it?
If there is any way to achieve it using volley?
You can run parallel JSONObjectRequests by setting their tags. For example lets say you want to send requests using loop. You will do something like following:
for (int i = 0; i < requestSize; i++)
{
StringRequest request = new StringRequest(Request.Method.POST,
AppUrls.URL_SAVE_SAVE_DATA, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
String status = "";
JSONObject json = null;
try {
json = new JSONObject(response);
if(json != null && json.has(JSONParsor.STATUS))
status = json.getString(JSONParsor.STATUS);
if(status != null && status.length() > 0 && status.equalsIgnoreCase("success"))
{
}
else {
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
})
{
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put(JSONParsor.DATA_STRING, jsonObject.toString());
return params;
}
};
request.setTag("TAG_" + i);
addToRequestQueue(request);
}
In response, you can return the request id and then you can match, which request belongs to which tag.
Hope this helps.