0

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);
    }
lebarillier
  • 5,294
  • 1
  • 9
  • 14
  • can post your more code – Zaid Mirza Dec 04 '18 at 05:23
  • sure, I just edited it – lebarillier Dec 04 '18 at 05:41
  • you are trying to compare in a wrong way. – Zaid Mirza Dec 04 '18 at 06:38
  • https://www.tutorialspoint.com/android/android_json_parser.htm – Zaid Mirza Dec 04 '18 at 06:41
  • you have to read basic concepts – Zaid Mirza Dec 04 '18 at 06:41
  • ho, no, like I said I manage the server too, so when I use the string request I don't send a Json, just a string, but when I use the string request my string have 2 quotes added and I don't know why. I send "nofind" and receive ""nofind"" – lebarillier Dec 04 '18 at 06:53
  • 1
    however, you are comparing string in wrong way `response != "notified"` . please check this post https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Zaid Mirza Dec 04 '18 at 07:24
  • hoo I see, yeah i forgot that, thanks. but even with this that don't work, even if i try with "\"something\"" I don't know why I have this quotes added... – lebarillier Dec 04 '18 at 07:52
  • 1
    do not worry about that quotes, they are formatted by json view from where you copied it. just assume there is no quote then try. – Zaid Mirza Dec 04 '18 at 07:56
  • or maybe you are sending response with quotes – Zaid Mirza Dec 04 '18 at 07:57
  • I will try again, for know the application crash because of a new activity, i solve this issue and I come back Thank for your time :) – lebarillier Dec 04 '18 at 07:59
  • ok, I changed the (respons != "nofind") by (!respons.equals("nofind")) but still the same that change nothings... and since you told me that the quotes come from the json, I asked the servant to send a simple string, and now I receive `nofind` – lebarillier Dec 04 '18 at 08:24
  • It's OK I find the solution, I had try with (respons != "\"nofind\"") and (!respons.equals("nofind")) but the answer is (!respons.equals("\"nofind\"")). So I still have the quotes but I can do what I want. So thanks to you, I'm feeling a little dumb to don't find this solution alone. – lebarillier Dec 04 '18 at 08:28

1 Answers1

0

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

lebarillier
  • 5,294
  • 1
  • 9
  • 14