2012-02-06 40 views
0

我嘗試使用this網站上的類來實現通過Web服務登錄的方式。每當我嘗試登錄我的測試程序時,我都會在登錄嘗試後設置輸出以「響應」。該程序停頓約一秒鐘,然後輸出一個空字符串。有什麼方法可以查看我的應用程序是否嘗試登錄?甚至成功連接到互聯網?我的應用程序未成功登錄到Web服務

下面的代碼片段:

private RestClient restClient = new RestClient("https://service...."); 
... 
private void LoginCheck() 
{ 

    restClient.AddParam("username", username); 
    restClient.AddParam("password", userpass); 

    try { 
     restClient.Execute(RequestMethod.POST); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    String response = restClient.getResponse(); 
    Toast.makeText(LoginActivity.this, response, Toast.LENGTH_SHORT).show(); 
} 
+2

您是否已將INTERNET權限添加到清單中? – 2012-02-06 18:02:19

+0

是的。在我做完之後,程序在返回之前會暫停一會兒。 – CodePrimate 2012-02-06 20:40:49

回答

0

我認爲你正在嘗試做的HTTPPost,你可以這樣做的,它爲我工作。

try { 
    HttpClient client = new DefaultHttpClient(); 
    String postURL = "https://service...."; 
    HttpPost post = new HttpPost(postURL); 
     List<NameValuePair> params = new ArrayList<NameValuePair>(2); 
     params.add(new BasicNameValuePair("username", "foo")); 
     params.add(new BasicNameValuePair("password", "bar")); 
     UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8); 
     post.setEntity(ent); 
     HttpResponse responsePOST = client.execute(post); 
     HttpEntity resEntity = responsePOST.getEntity(); 
     if (resEntity != null) {  
      String response = EntityUtils.toString(resEntity)); 
     } 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
相關問題