2012-07-12 38 views
0

由於他們改變了我們在最新的Android SDK中發出HTTP請求的方式,因此我一直無法找到顯示如何發出HTTP發佈請求的教程。特別是用於登錄。所以我希望看到一些代碼示例,展示如何實現HTTP post請求並處理cookie。如果可能的話,我也想看看代碼示例https://security.stackexchange.com/questions/4302/how-to-implement-a-remember-me-on-a-mobile-app謝謝。如何實現Android 4.0 Restful,HTTP發佈請求

回答

2

圖書館使用:http://loopj.com/android-async-http/

private OnClickListener login = new OnClickListener() { 

    public void onClick(View view) { 

     AsyncHttpClient myClient = new AsyncHttpClient(); 
     myClient.get(URL, null); 
     myClient.setCookieStore(myCookieStore); 
     myClient.setCookieStore(myCookieStore); 
     String username = ""; 
     String password = ""; 
     RequestParams params1 = new RequestParams(); 
     params1.put("username", username); 
     params1.put("password", password); 
     pd = ProgressDialog.show(this, "", "Signing In..."); 
     myClient.post(URL, params1, 
       new AsyncHttpResponseHandler() { 
        @Override 
        public void onSuccess(String response) { 
         System.out.println("response" + response); 
         pd.dismiss(); 
         if (response.contains("<!--Authorized-->")) { 
         } 
         else { 
          pd.dismiss(); 
          Context mContext = SigninActivity.this; 
          notMatchDialog = new Dialog(mContext); 
          notMatchDialog.setContentView(R.layout.loginfaileddialoglayout); 
          notMatchDialog.setTitle("Login failed"); 
          dismissDialogButton = (Button) notMatchDialog.findViewById(R.id.dismissDialogButton); 
          dismissDialogButton.setOnClickListener(dismissDialog); 
          notMatchDialog.show(); 
         } 
        } 

        @Override 
        public void onFailure(Throwable e, String response) { 
         // TODO Need to figure out different failures and try to help the user. 
        } 
       }); 
    } 
}; 
+0

謝謝....大 – Dave 2015-04-03 17:13:19