I don't even know I am right are wrong. My situation of thinking is currently null. can some please tell me how to update deliverystatus
This is code in AdapterClass
delivered.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String serverURL= PathUrls.pathUrl+"evs_updatedeliverystatus.php?db="+companyName.getString("companyName","")+"&invoiceid="+dlb.getInvoiceNo()+"&deliverystatus=1";
JsonObjectRequest jsonArrayRequest=new JsonObjectRequest(serverURL, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d("update delivery list",response.toString());
deliveryListBeans.clear();
notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error DeliveryList:%n %s ", error);
Toast.makeText(ct,"NetworkError Not Responding", Toast.LENGTH_SHORT).show();
}
});
//Toast.makeText(ct, "companyName"+companyName.getString("companyName","").toString(), Toast.LENGTH_LONG).show();
VolleySingleton.getsInstance().getRequestQueue().add(jsonArrayRequest);
}
});
I already tested PHP with its URL it is working fine
I think you should check data follow
1 . Make sure serverURL is correct.
2 . While connecting to server you already call UpdateDeliveryList Activity. It will destroy current Activity
3 . When server return response successful:
deliveryListBeans.clear();// Clear list
notifyDataSetChanged(); // and update list
I dont see you get deliverystatus from onResponse()
Related
I am a new guy in android and trying to make a rest api call using volley.
I am getting "com.android.volley.ClientError" in my code. Can anyone help me to solve this please.
private void LoginByNet(String uID, String pSD){
String URL = "http://myipaddress:65017/api/values";
RequestQueue rq = Volley.newRequestQueue(this);
JsonObjectRequest jq = new JsonObjectRequest(
Request.Method.GET,
URL,
null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Toast.makeText(getApplicationContext(), "Success:"+response.toString(), Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),"Error:"+ error.toString(), Toast.LENGTH_SHORT).show();
userName.setText(error.toString());
}
}
);
rq.add(jq);
}
This means that the server returned a 4xx error code.
[https://github.com/google/volley/blob/d1a3d5388c79ff1a6fdb904daeff9b39d0bb7d26/src/main/java/com/android/volley/toolbox/BasicNetwork.java#L199][1]
You can get the exact error code using : #error.networkResponse.statusCode
I don't even know I am right are wrong. My situation of thinking is currently null. can some please tell me how to update deliverystatus
This is code in AdapterClass
delivered.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String serverURL= PathUrls.pathUrl+"evs_updatedeliverystatus.php?db="+companyName.getString("companyName","")+"&invoiceid="+dlb.getInvoiceNo()+"&deliverystatus=1";
JsonObjectRequest jsonArrayRequest=new JsonObjectRequest(serverURL, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d("update delivery list",response.toString());
deliveryListBeans.clear();
notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error DeliveryList:%n %s ", error);
Toast.makeText(ct,"NetworkError Not Responding", Toast.LENGTH_SHORT).show();
}
});
//Toast.makeText(ct, "companyName"+companyName.getString("companyName","").toString(), Toast.LENGTH_LONG).show();
VolleySingleton.getsInstance().getRequestQueue().add(jsonArrayRequest);
}
});
I want to execute JSON_requestarray in my android app but its not working, here is the code:
I want to extract the contents from the array which my PHP returns as a Json_encoded array.. but every time I run it returns null
JsonArrayRequest does not support POST method.
JsonArrayRequest req = new JsonArrayRequest("http://url", new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray jsonArray) {
Log.d("response", jsonArray.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
}
});
If the log "response" isn't correct maybe the problem is your web service.
I am trying to understand how volley is used in android, So in my project I am querying a single name from http://uinames.com/api/?amount=1
In the app I have a button that is calling the function to make the request and then set it to the TextView. Also for debug Purposes I am logging the response from the url. On the button click the response is correctly seen in the logcat but the text is not set to the TextView until the next time the button is clicked.
Here is the code for the JsonObjectRequest
public String getName() {
JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.GET,
URL, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
full_name = response.getString(name) + " " + response.getString(last);
Log.d(TAG, full_name);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
AppController.getInstance().addToRequestQueue(objectRequest, "");
return full_name;
}
Following is the Code from the MainActivity that calls the method getName()
mButton = (Button) findViewById(R.id.btn);
mTextView = (TextView) findViewById(R.id.text);
final RequestClass newReq = new RequestClass();
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
name = newReq.getName();
mTextView.setText(name);
}
});
Here is what happens when your function String getName() is called:
A JsonObjectRequest is constructed, and a response and error listeners are passed to the request. Note that the listeners are not executed until their corresponding events happen.
The JsonObjectRequest is added to the request queue, to be executed when a thread from the thread pool is available.
Until now the full_name variable has not been touched, and you return the last value that was assigned to it (that's why when you click the button again, you get a name in your text view).
Your request gets a response and the listener is called assigning a new value to full_name after your function has returned. that is the value that you get on the next call to getName(). . .
In general, a function like getName can not perform a network operation and return its result immediately. It either needs to block until it has the response from the server (and this is prohibited in android). Or, it can return without the result being fetched yet, and have a callback function (or a listener) that is called with the result when it is available (and this is the the general approach in the Volley library).
That means that you have to setText in your response listener. I have noticed that you have a separated class for your network requests RequestClass, to keep your design as it is, one solution would be to pass the listeners to getName() function (and all other functions that do network stuff in this class), it may look something like this:
public void getName(Response.Listener<JSONObject> responseListener,
Response.ErrorListener errorListener) {
JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.GET,
URL, null, responseListener, errorListener);
AppController.getInstance().addToRequestQueue(objectRequest, "");
}
And in your activity, you have to pass listeners like this:
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
name = newReq.getName(
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
String full_name = response.getString("name") + " " + response.getString("last");
mTextView.setText(full_name);
Log.d(TAG, full_name);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
}
});
As a better design, you may want to avoid passing Volley listeners from the activity (which does not have to know anything about volley), so you may want to define your own listener interface, override and pass that from the activity to your networking class (where you instantiate your Response.Listener, Response.ErrorListener and override them to call functions in your listener).
Volley library is deprecated so don't use Volley library.
The best alternative for Volley is RetroFit library.
Try like this
public void getName() {
JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.GET,
URL, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
full_name = response.getString("name") + " " + response.getString("last");
runOnUiThread(new Runnable() {
#Override
public void run() {
//Your code to run in GUI thread here
mTextView.setText(full_name);
}
});
Log.d(TAG, full_name);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
AppController.getInstance().addToRequestQueue(objectRequest, "");
}
mButton = (Button) findViewById(R.id.btn);
mTextView = (TextView) findViewById(R.id.text);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getName();
}
});
I need to make a JSON Request and set UI depending on its response, so it would nice to make it in onCreate in the same thread.
Speaking about RequestFuture, I can't reliably use it for this, or I don't know how to do it.
RequestFuture<JSONObject> future = RequestFuture.newFuture();
JsonObjectRequest request = new JsonObjectRequest(URL, null, future, future);
requestQueue.add(request);
try {
JSONObject response = future.get();
count = response.getIn("int");
} catch (InterruptedException e) {
} catch (ExecutionException e) {
}catch (JSONException e) {
Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_LONG).show();
}
I tried this, but its dont work.
If someone know pls show an example how to get data from JSONObject and dont do nothing befor data is goten and than use that values to set some Views.
as there is getActivity() in your code, i bet you are in a fragment.
to do something on UI thread you could call something like this
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
// your UI-related code goes here
}
});
Using Volley you have listeners of response when succeed and an error listener when server has a respone with error code (500, 401).
So you can create a JSONObjectRequest like the following code lines:
JSONObjectRequest jsObjRequest = new JsonArrayRequest
(REQUEST_URL, new Response.Listener<JSONObjectRequest>() {
#Override
public void onResponse(JSONObjectRequest response) {
// this code will be executed when response comes and you can set your views, the way is depending where you are (fragment, activity, etc), you can do runOnUiThread
Log.d("Hello", "You can do whatever you want wen data is goten");
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("Error","You can show an error dialog here ");
}
});
You have to add the above object created to the HTTQueue and it will be executed at its time. Hope it helped !
if you are using volley then you can use the listeners. You can also refer this https://developer.android.com/training/volley/simple.html . In onResponse or onError you can do the UI changes that you want.
JsonArrayRequest request = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}
});