How do you to implement Android 4.0 Restful, HTTP

2019-06-09 15:29发布

Since they changed the way we make HTTP request in the latest Android SDK, I have not been able to find a tutorial that shows how to make HTTP post request. Especially for login. So I would like to see a some code examples that show how to implement an HTTP post request, and deals with the cookies. And if possible, I would also like to see a code example for https://security.stackexchange.com/questions/4302/how-to-implement-a-remember-me-on-a-mobile-app Thank you.

1条回答
够拽才男人
2楼-- · 2019-06-09 15:45

Library used: 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.
                    }
                });
    }
};
查看更多
登录 后发表回答