2011-09-08 4 views
0

Je ai une question que lorsque nous voulons publier une demande de JSON à un serveur, il renvoie toujours [email protected] lorsque nous voulons imprimer la réponse, alors quel est les moyens de cette réponse?Réponse de json post

code:

HttpClient client = new DefaultHttpClient(); 
        HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit 
        HttpResponse response; 
        JSONObject json = new JSONObject(); 
        try{ 
         HttpPost post = new HttpPost("http://122.180.114.68/eqixmobile/siteservice/um/ibx"); 
         json.put("username", ed_usrName.getText().toString()); 
         json.put("password", ed_pass.getText().toString()); 
         post.setHeader("Content-Type", "application/json"); 
         post.setHeader("Accept", "application/json"); 
         StringEntity se = new StringEntity("credentials: " + json.toString()); 
         se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 
         post.setEntity(se); 
         response = client.execute(post); 
         /*Checking response */ 
         if(response!=null){ 
          InputStream in = response.getEntity().getContent(); 
          //String str = response.getEntity().getContent().toString();//Get the data in the entity 
          System.out.println("This is service response:"+in); 
        } 
         else 
         { 
          System.out.println("This is no any service response:"); 
         } 
        } 
        catch(Exception e){ 
         e.printStackTrace(); 
         //createDialog("Error", "Cannot Estabilish Connection"); 
        } 
+0

avez-vous donner la permission Internet premier? –

Répondre

3

La réponse que vous obtenez de serveur est en inputstream donc convertir en chaîne, utilisez-le. comme,

String result=null; 
InputStream is = entity.getContent(); 
      // convert stream to string 
      result = convertStreamToString(is); 
      result = result.replace("\n", ""); 

public static String convertStreamToString(InputStream is) throws Exception { 

    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); 
    StringBuilder sb = new StringBuilder(); 
    String line = null; 
    while ((line = reader.readLine()) != null) { 
     sb.append(line + "\n"); 
    } 
    is.close(); 
    return sb.toString(); 
} 
0

Je recommande d'utiliser ces méthodes pour obtenir une réponse JSON

pour Get Méthode:

public class HttpManager { 

public static String getData(String uri) { 

    BufferedReader reader = null; 

    try { 
     URL url = new URL(uri); 
     HttpURLConnection con = (HttpURLConnection) url.openConnection(); 

     StringBuilder sb = new StringBuilder(); 
     reader = new BufferedReader(new InputStreamReader(con.getInputStream())); 

     String line; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 

     return sb.toString(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
     return null; 
    } finally { 
     if (reader != null) { 
      try { 
       reader.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
       return null; 
      } 
     } 
    } 

} 

} 

Pour méthode Post:

public class HttpRequest { 
public static String getData(String uri, String params) { 

    BufferedReader reader = null; 

    try { 
     URL url = new URL(uri); 
     HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
     con.setRequestMethod("POST"); 
     con.setDoOutput(true); 
     OutputStreamWriter writer = new OutputStreamWriter(
       con.getOutputStream()); 
     writer.write(params); 
     writer.flush(); 

     StringBuilder sb = new StringBuilder(); 
     reader = new BufferedReader(new InputStreamReader(
       con.getInputStream())); 

     String line; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 

     return sb.toString(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
     Log.i("exception", "" + e.getMessage()); 
     return null; 
    } finally { 
     if (reader != null) { 
      try { 
       reader.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
       return null; 
      } 
     } 
    } 

} 

}