Search code examples
androidhttpandroid-volleyjsonobjectrequest

receiving JSONobject allways failed with Voley


I have a problem when I try to use volley to communicate with a server (I created the server, so I can change a thing here too).
So when I use the stringRequest I have this problem:
my string contains 2 quotes, for example, the string looks like this: ""something"" while I send "something" and when I try to use this data like

if (response == "\"something\"")
     do something

that doesn't work. I just can't use this string normally.

And when I try to use JsonObjectRequest I don't know why but I allways have this issue:

org.json.JSONException: Value {"name":"nofind"} of type java.lang.String cannot be converted to JSONObject

I tried to send this :

"{\"name\":\"nofind\"}",  
"{'name':'nofind'}",  
"{name:\"nofind\"}",  
"{name:'nofind'}"  

But It's always the same problem. I don't know why.
So please, if someone can help me. I will be very grateful

EDIT :
here my code :
JsonObjectRequest :

JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, URL_SERVER+URL_IMAGE,
            null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                resultsTextView.setText(response.toString());
                snackbar.dismiss();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("ERROR","error => "+error.toString());
                    resultsTextView.setText(R.string.ErrorServor);
                    snackbar.dismiss();
                }
            }){
        @Override
        public byte[] getBody(){
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            faceBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();
            faceBitmap.recycle();
            return byteArray ;
        }

        @Override
        public String getBodyContentType() {
            return "image/jpeg";
        }
    };
    request.setRetryPolicy(new DefaultRetryPolicy(
            0,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    queue.add(request);

String request:

StringRequest request = new StringRequest(Request.Method.POST, URL_SERVER + URL_IMAGE,
            this, this){

        @Override
        public byte[] getBody() {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            faceBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();
            faceBitmap.recycle();
            return byteArray ;
        }

        @Override
        public String getBodyContentType() {
            return "image/jpeg";
        }
    };
    request.setRetryPolicy(new DefaultRetryPolicy(
            0,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    queue.add(request);
}


@Override
public void onErrorResponse(VolleyError error) {
    Log.d("ERROR","error => "+error.toString());
    resultsTextView.setText(R.string.ErrorServor);
    snackbar.dismiss();
}

@Override
public void onResponse(String response) {
    try {
        if (response != "nofind")
        {
            resultsTextView.setText(response);
            NoButton.setVisibility(View.VISIBLE);
            YesButton.setVisibility(View.VISIBLE);
        }
        else {
            resultsTextView.setText(R.string.NoBack);
        }
        resultsTextView.setText(obj.toString());
    } catch (JSONException e) {
        e.printStackTrace();
        System.out.println(e);
    }

Solution

  • For each people who have the same problem as me.
    To establish a communication between a WCF server and an Android application with Volley.
    For the StringRequest the solution is written above.I just had to use .equals(MyString) in place of ==MyString.
    But for the JsonObjectRequest the problem was on the server. In the BodyStyle parameter. the Solution is I had to Wrappe Only the response.
    So this is the function who worked for me.

    [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "/NewUser",BodyStyle = WebMessageBodyStyle.WrappedResponse, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        dynamic NewUser(User user);
    

    More information About the BodyStyle here : RESTful web service body format