2013-01-05 19 views
0

Im編寫一個RESTful API & Android客戶端在我去的同時,我正在從服務器拉取用戶配置文件。我覺得這應該肯定是一個獲取請求,即時通訊只提取現有數據,即時通訊不向數據庫添加/編輯任何內容,但我需要一個user_id參數才能查詢相應的配置文件。我可以只發送一個微小的變量和我的HttpGet一起,在這種情況下我該如何還是應該使用HttpPost?我可以用1個字符串參數發送Android HttpGet(不是HttpPost)嗎?

回答

0

Android使用Apache's HTTPClient。所以,複製他們的tutorial code

public void sendStringTo(String remoteUrl, String myString) { 
    DefaultHttpClient httpclient = new DefaultHttpClient(); 
    HttpGet httpGet = new HttpGet(remoteUrl+"?string1="+myString); 
    HttpResponse response1 = httpclient.execute(httpGet); 

    // The underlying HTTP connection is still held by the response object 
    // to allow the response content to be streamed directly from the network socket. 
    // In order to ensure correct deallocation of system resources 
    // the user MUST either fully consume the response content or abort request 
    // execution by calling HttpGet#releaseConnection(). 

    try { 
     System.out.println(response1.getStatusLine()); 
     HttpEntity entity1 = response1.getEntity(); 
     // do something useful with the response body 
     // and ensure it is fully consumed 
     EntityUtils.consume(entity1); 
    } finally { 
     httpGet.releaseConnection(); 
    } 
    return; 
} 
0

GET可以支持添加變量/參數。例如,你可以做一個Url看起來像這樣:

http://yourwebsite.com/script.php?user_id=19898424

相關問題