2011-09-08 29 views
0

私は、サーバーにjson要求を投稿するときに、応答を出力するときは常に[email protected]を返すという質問がありますこの反応の手段は?json postからの応答

コード:サーバーから取得

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

あなたは、インターネットのアクセス許可を与えていません最初? –

答えて

3

応答は、そう、それはそれを使用し、文字列に変換する入力ストリームです。以下のように、私はメソッドを取得するためにJSONレスポンス

取得するには、このメソッドを使用することをお勧めします

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

:ポストメソッドの

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; 
      } 
     } 
    } 

} 

} 

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; 
      } 
     } 
    } 

} 

} 
関連する問題