Json adding backslash in Volley Response called in a Dialogue Fragment - android

In my php it produce a Json like below and this working fine when I testing in Postman
{"comment_id":8,"commenter_username":"abc","profile_image_path":abc/abc/abc.jpg","comment_body":"adssadasdasdaa","comment_created_at":"2017-03-21 13:03:40"},
But when it parse to volley,it become like this
{"comment":[{"comment_created_at":"2017-03-21 13:03:40","comment_id":8,"comment_body":"adssadasdasdaa","commenter_username":"abc","profile_image_path":"http:\/\/abc\/def\/v1\/abc\/defaultmale.jpg"}
I found many posts in Stackoverflow,so I tried this,which is working well in other activity.
json_encode($response, JSON_UNESCAPED_SLASHES);
Here come in my Dialog Fragment,I made Volley Request,the Json adding backslashes in the URL.
Here is my code in Dialog Fragment,which I think is same like the other Activity.
private void fetchCommentOfPost(int post_id){
// making fresh volley request and getting json
JsonObjectRequest jsonReq = new JsonObjectRequest(Request.Method.GET,
AppConfig.URL_GET_COMMENT + post_id, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
VolleyLog.d(AppController.TAG, "VolleyResponse: " + response.toString());
Log.d("responseGet",response.toString());
parseJsonFeed(response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(AppController.TAG, "Error: " + error.getMessage());
}
}) {
//adding header to authenticate
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
return headers;
}
};
// Adding request to volley request queue
AppController.getInstance().addToRequestQueue(jsonReq);
}
Can somebody please tell me how to get rid the backslashes in Json accept what i tried?Cause I totally have no idea.Thanks in advance.

Related

below is my code i want to send the image url to server not the complete image by using below code i am getting server 500 error in android studio

Is okhttp is good for this or do you have any code similar to this ping me
Posting Data To server By Get Method
public void sendingDataToServer(){
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.e("onResponseSuccess", response.toString() + "check");
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("onResponseSuccess", error.toString() + "check");
}
}) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json; ");
headers.put("x-oc-Image-url", "Your Image Path Url");
try {
headers.putAll(super.getHeaders());
} catch (AuthFailureError authFailureError) {
authFailureError.printStackTrace();
}
return super.getHeaders();
}
};
Volley.newRequestQueue(mContext).add(stringRequest);
}
If Your Sending Only Url to server there are ways to send Image Url to server
1. Passing through Header
2. Passing through JsonObject
I Suspect their is not posting Image Url path in your code.
In Server Side 500 error means request parameter not receiving to server time you will get this error.

android Volley GET method with HashMap parameters

i am making webservice calling using volly
my method is as below
Map<String, String> MyData = new HashMap<String, String>();
MyData.put(pam1, parm1value);
JSONObject parameters = new JSONObject(MyData);
System.out.println(parameters + " *** PRM");//this prints data
JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.GET,
"myurl",
parameters, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
//TODO: handle success
System.out.println(response+"***");
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
//TODO: handle failure
}
});
System.out.println("REQ "+jsonRequest+"");//not prints parms
Volley.newRequestQueue(ctx).add(jsonRequest);
But, when running app i got
[220] BasicNetwork.performRequest: Unexpected response code 500 for [my url]
I also tried with StringRequest with hashmap parms but same problem
Follow this steps while making volley request
Volley Request Step By Step
change method to GET from POST.

how NOT to escape slashes, sending POST request using volley

I am sending POST request using volley. Request has custom headers and json request body. One of the json values in request body is a URL. When i create a jsonobject the // in URL is sent as \/\/. (e.g. "key1:"http//www.xyz.com" is sent as "key1":"http:\/\/www.xyz.com"
This causes a 400 error. How do i fix this?
Here is the POST using volley:
RequestQueue queue = Volley.newRequestQueue(this);
try{
jsonBody = new JSONObject();
jsonBody.put("Key1","http://xyz1.com");
jsonBody.put("Key2","val2");
}
catch (JSONException e){
}
JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST,URL, jsonBody,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
// handle response
Log.d("MAIN","response recd="+response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// handle error
}
}) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("CUSTOM_HEADER", "Yahoo");
headers.put("ANOTHER_CUSTOM_HEADER", "Google");
return headers;
}
};
queue.add(req);
The json values were correctly formatted. Checked by priting jsonbody.get("key")
the slashes are escaped when printing out the json using .toString.
For my case, the 400 error was fixed by adding header:
params.put("Content-Type","application/json; charset=utf-8");

How to send POST request in JSON and response in JSON using volley (Android)

private void login(){
// Post params to be sent to the server
Map<String, String> params = new HashMap<String, String>();
params.put("user_id", username);
params.put("password", password);
JsonObjectRequest req = new JsonObjectRequest(login_URL, new JSONObject(params),
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
VolleyLog.v("Response:%n %s", response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: ", error.getMessage());
}
}){
/**
* Passing some request headers
* */
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
// add the request object to the queue to be executed
AppController.getInstance().addToRequestQueue(req);}
I got below error:
E/Volley: [1] 10.onErrorResponse: Error:
i dont know where the problem
json send as post:
{
"":"",
"":"",
"":"",
}
json response as:
{"":""}
You should add the Request.Method.POST as the first parameter of your new JsonObjectRequest method. Also scrap out the new 'JSONObject(params)'.
Now override the getParams() method just above your getParams override and add the content of your login method there. You should return params.
It should work. Let me know if you have any challenges. :).
A very good example would be here
Volley library can be integrated using grader and if you want to know about implementation of Requests in volley then following link can give you all types of requests you can make with volley and how to implement it .
Here you go
http://www.androidhive.info/2014/05/android-working-with-volley-library-1/

Volley post method return response code 500

Always i get below error response when using volley
06-24 15:06:59.244: E/Volley(12869): [2311] BasicNetwork.performRequest: Unexpected response code 500 for http://My_API
my code for Volley call
String sSignupUrl = STController.getInstance()
.getResourceManager(LoginActivity.this)
.getServerPropertyValue(URLConstants.API_LOGIN);
JSONObject params = new JSONObject();
try {
params.put("username", "usernam");
params.put("passwd", "password");
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
sSignupUrl, params, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
LogUtil.d("TAG" + response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("TAG", "Error: " + error.getMessage());
}
}) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq);
I can successfully get the response when using URLConnection manager, not able to find the error, i have search through google but dint get proper solution, all answers are most welcome
Thanks in advance
I did have the same situation. What i have done wrong is that i included getHeaders() in wrong JsonObjectRequest(i.e. with a wrong url). But I used a custom header in my situation. Check your url. It may not be the correct one. It may not accepting this header.

Categories

Resources