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!