在我的应用中。我必须使用HTTPPost方法注册一个新用户。我为此编写了以下代码。但是有时我得到如下的回复:
代码:
String Category_url = "url";
try
{
SimpleDateFormat f = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss");
f.setTimeZone(TimeZone.getTimeZone("UTC"));
String signature_string = "POST"+"users/register/"+"global.se"+String.valueOf(f.format(new Date())+" +0000");
Log.d("signature_string", signature_string);
String auth_key = hmacSha1(signature_string, "1232132123213244353");
Log.d("auth_key", auth_key);
HttpClient client = new DefaultHttpClient();
HttpPost get = new HttpPost(Category_url);
List nvps = new ArrayList();
nvps.add(new BasicNameValuePair("email", "mohit.kanada@gmail.com"));
nvps.add(new BasicNameValuePair("password", "123456"));
nvps.add(new BasicNameValuePair("first_name", "Mohit"));
nvps.add(new BasicNameValuePair("last_name", "Kanada"));
UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
get.setEntity(p_entity);
get.addHeader("Host", "HOST_name");
get.addHeader("X-SE-Date", String.valueOf(f.format(new Date()))+" +0000");
get.addHeader("X-SE-Client", "client_name");
get.addHeader("X-SE-Accept", "xml");
get.addHeader("X-SE-Auth", auth_key);
HttpResponse httpresponse = client.execute(get);
HttpEntity httpentity = httpresponse.getEntity();
String s = EntityUtils.toString(httpentity);
Log.d("login response", s);
}
catch (Exception e)
{
}
响应:
<?xml version="1.0" encoding="UTF-8"?>
<error>
<code>InvalidParameter</code>
<message>Invalid parameter specified (email)</message>
</error>
</xml>
如果我没有传递任何参数,那么我也得到相同的响应。使用此参数,我可以使用浏览器在线注册新用户。
我认为web服务器无法获取我的参数,因为参数名称的任何变化都不会影响响应。
答案 0 :(得分:0)
使用的图书馆: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.
}
});
}
};