由于他们改变了我们在最新的 Android SDK 中发出 HTTP 请求的方式,我一直无法找到说明如何发出 HTTP post 请求的教程。尤其是登录。所以我想看一些代码示例,展示如何实现 HTTP 发布请求,并处理 cookie。如果可能的话,我还想查看https://security.stackexchange.com/questions/4302/how-to-implement-a-remember-me-on-a-mobile-app的代码示例谢谢。
问问题
1333 次
1 回答
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.
}
});
}
};
于 2012-07-15T01:40:29.377 回答