2011-09-25 62 views
0

我的應用連接到服務器,一旦服務器收到數據,它會發送一個哈希碼作爲響應...我的問題是如何獲得這個哈希碼?Android-從服務器獲取哈希碼

我用下面的代碼來發布數據:

try { 
    HttpClient client = new DefaultHttpClient(); 
    String postURL = "http://somepostaddress.com"; 
    HttpPost post = new HttpPost(postURL); 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("user", "kris")); 
     params.add(new BasicNameValuePair("pass", "xyz")); 
     UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8); 
     post.setEntity(ent); 
     HttpResponse responsePOST = client.execute(post); 
     HttpEntity resEntity = responsePOST.getEntity(); 
     if (resEntity != null) {  
      Log.i("RESPONSE",EntityUtils.toString(resEntity)); 
     } 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

回答

0
URL url = new URL(url_str); 
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
connection.setRequestMethod("GET"); 
connection.connect(); 
InputStream stream = connection.getInputStream(); 

這就是我爲GET所做的,它也適用於POST。

0

使用EntityUtils.toString(HttpEntity),這個返回響應爲String

+0

好的,但如何獲得哈希碼? – Dev

+0

String hashCode = EntityUtils.toString(resEntity); – aromero

+0

這假設你在迴應中以純文本的形式返回哈希碼或任何字符串。 – aromero