How to send JSONObject to a Web Service?

3

I'm trying for the first time to consume a Web Service in my Android application, sending data in JSON format.

To accomplish the task I have a class to make the connection.

public class ConexaoHttpJson {

public static JSONObject enviarSolicitacao(String urlPost,JSONObject obj) throws ClientProtocolException, IOException, JSONException {      

    HttpContext localContext = new BasicHttpContext();
    HttpClient client = new DefaultHttpClient();  
    HttpPost post = new HttpPost(urlPost); 
    post.setHeader("Content-type", "application/json");
    //post.setHeader("Authorization",token);
    //post.setHeader("Cookie","ASP.NET_SessionId="+sessao+"; path=/; HttpOnly");

    post.setEntity(new StringEntity(obj.toString()));
    HttpResponse response = client.execute(post,localContext);  

    HttpEntity entity = response.getEntity();
    InputStream instream = entity.getContent();

    String resultString= convertStreamToString(instream);

    JSONObject jsonObjRecv = new JSONObject(resultString);

    Log.i("json servidor", jsonObjRecv.toString());
    instream.close();

    return  jsonObjRecv;
}

private static String convertStreamToString(InputStream is) {

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }           
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {               
            e.printStackTrace();            
        }
    }
    return sb.toString();
}
}

Now I need HTML and ObjectJson , which I'm creating in a Activity class, in this format:

    public void onClickConnect(View view) {
        new Thread() {
            @Override
            public void run() {
            super.run();
            try {                       
                 JSONObject json = new JSONObject();
                 json.put("email", login.getText().toString()); 
                 json.put("password", senha.getText().toString());

                 String url = "http://webservice.com/Api.php?user=logar";//EXEMPLO
                 JSONObject response = ConexaoHttpJson.enviarSolicitacao(url, json);                           
                 } catch (ClientProtocolException e) {
                     e.printStackTrace();
                 } catch (IOException e) {
                     e.printStackTrace();
                 } catch (JSONException e) {
                     e.printStackTrace();
                 }
            }
        }.start();   
    }

I want to send a URL, and email and password in JSON format, pick up the response from the WebService and work on it.

The problem is in the line of the enviarSolicitação method.

   String resultString= convertStreamToString(instream);        
   JSONObject jsonObjRecv = new JSONObject(resultString);

Error Log

05-13 22:48:25.430: W/System.err(1951): org.json.JSONException: Value <meta of type java.lang.String cannot be converted to JSONObject
05-13 22:48:25.430: W/System.err(1951):     at org.json.JSON.typeMismatch(JSON.java:111)
05-13 22:48:25.430: W/System.err(1951):     at org.json.JSONObject.<init>(JSONObject.java:158)
05-13 22:48:25.430: W/System.err(1951):     at org.json.JSONObject.<init>(JSONObject.java:171)
05-13 22:48:25.430: W/System.err(1951):     at com.sostudy.ConexaoHttpJson.enviarSolicitacao(ConexaoHttpJson.java:44)
05-13 22:48:25.430: W/System.err(1951):     at com.sostudy.MainActivity$1.run(MainActivity.java:153)
    
asked by anonymous 13.05.2014 / 20:47

2 answers

2

To use this line of code, the value of resultString must be a valid JSON.

JSONObject jsonObjRecv = new JSONObject(resultString) 

A JSON is an object by key, value in which the key has to be a string and the value can be a string , a number, a boolean (true or false), a JSON object, or a string .

Probably your resultString is not a valid Json.

    
15.05.2014 / 12:11
0

To avoid having to deal directly with json in android and recomendabel to use GSON link avoid having these problems in manipulate string to create json

    
13.01.2015 / 04:18