Sending JSON to web-service via Http

Asked

Viewed 44 times

0

I have a question that I believe to be simple to solve, I have a web service that feeds my App, I can request this information but I can’t send information, the closest I have so far was:

JSONObject jsonParams = new JSONObject();
    try {
        jsonParams.put("teste", 1);

    } catch (JSONException e) {
        e.printStackTrace();
    }
    try {
        Log.d("JSON",jsonParams.getString("teste"));
    } catch (JSONException e) {
        e.printStackTrace();
    }
    RequestQueue queue = Volley.newRequestQueue(contexto);
    //url aponta para um servidor local http://192.160.0.xxx:8080/web_server_sdc/
    String url =Comunicacao.getInstance().getIp()+"finalizar.php";

    // Request a string response from the provided URL.
    JsonObjectRequest joReq = new JsonObjectRequest(Request.Method.POST, url, jsonParams,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d("Resposta", response.toString());
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("Resposta", error.getMessage());

                }
            });

When I run this code snippet I get the following error:

D/Resposta:org.json.JSONException:Value < br of type java.lang.String 
  cannot be converted to JSONObject

In the web service you have the following code:

echo '{"erro":"erro"}';

Can someone give me a light? Even if it’s a link to some tutorial

  • Have you tried debugging the line that is giving error and see what value it is returning? In this file Voce is only making a request on the server. In case you are having difficulties.

  • Eventually I gave up making a JSON request and made a request using a String json, at least it worked...

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.