1

I'm currently working on an android app and I need your help.

When I try to Use JsonObjectRequest supported by Volley, I get an error:

com.android.volley.ParseError: org.json.JSONException: Value 1 of type java.lang.String cannot be converted to JSONObject

This are my two relevant code snippets:

Receiver.java

public void sendNewInfo(String url, HashMap<String , String> params) {
    JsonObjectRequest newInfoRequest = new JsonObjectRequest(Request.Method.POST, url, new JSONObject(params) , new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            //doing some fancy stuff here
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(newInfoFragment.getActivity(), "Error: " + error.getMessage(), Toast.LENGTH_LONG).show();
        }
    });
    queue.add(newInfoRequest);
}

NewInfoFragment.java

public void onViewCreated(final View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    Button btn = (Button) view.findViewById(R.id.btnSendeInfo);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText title = (EditText) view.findViewById(R.id.newInfoTitle);
            EditText text = (EditText) view.findViewById(R.id.newInfoText);

            HashMap<String , String> params = new HashMap<String , String>();
            params.put("appkey", "123");
            params.put("autor", ((NewInfo) getActivity()).getBenutzername());
            params.put("titel", title.getText().toString());
            params.put("text", text.getText().toString());

            Receiver receiver = new Receiver(infofragment);
            receiver.sendNewInfo("myurlstringhere", params);
        }
    });
}

Does somebody have a good solution or any idea how I can solve this? The params are sent into the Receiver and he cannot resolve them into a JsonObject but I don't know how to do this.

EDIT For all of you, who do have the same error, please recheck your JSON you get as response. If its not valid, Volley throws this exception!

fr3ddyf
  • 73
  • 8
  • What is the response you are getting? could you try using `AdvancedRestClient` or atleast please share the RestAPI URL you are using – Mithun May 24 '15 at 15:46
  • @Mithun the response I get is: Value 1 of type java.lang.String cannot be converted to JSONObject The RestAPI is use is coded by myself. When I access it from another place (Ajax call / php / ...) it works! – fr3ddyf May 24 '15 at 16:24
  • Your `request` and `response` both ideally should be in JSON format otherwise, `Volley` won't be able to parse `response` and hence it throws `ParseException`. Without RestAPI its very hard to pinpoint the issue, as there could be problem in your `request` structure as well `response` also. – Mithun May 24 '15 at 17:36
  • @Mithun In my opinion, both is JSON. I am creating a JSONObject and getting a JSONObject as response from my webservice. The data my webservice expects is a normal POST request with 4 variables, am I right that volley normally should do this? :/ – fr3ddyf May 25 '15 at 06:41
  • I didn't see your updated code earlier... Your request seems to be fine! You are creating `Map` and sending it wrapped in a `JSONObject`... But thing is **Value 1** is basically a `String` and not a `JSON`! But question is, if you use `AdvancedRestClient` what value are you getting? If it is a `JSON` response there, then definitely There is some issue how we are sending our request and if not `Volley` has to be looked into for better debugging... See http://stackoverflow.com/questions/30196601/volley-string-cannot-be-converted-to-jsonobject/30197539#30197539 if it gives some more info – Mithun May 25 '15 at 07:00
  • See this screenshot: http://imgur.com/l4N3vC7 It works fine! "1" is the expected response... – fr3ddyf May 25 '15 at 07:14
  • I am at office now... can we continue may be after 2 hours from now? But **"1"** is definitely a valid json... Try validating *"1"* in http://jsonlint.com/ and you can see it – Mithun May 25 '15 at 07:27
  • Thanks man, I got it. The problem was an unvalid JSON from my Webservice :/ Didnt expect volley to be so strict. Thanks @Mithun – fr3ddyf May 25 '15 at 07:44
  • Check this answer [Volley JsonObjectRequest Post request not working](http://stackoverflow.com/a/19945676/3512164) – Ibrahim Disouki Oct 19 '15 at 11:18

0 Answers0