【问题标题】:HTTPPost parameter sending problemHTTPPost参数发送问题
【发布时间】:2011-07-26 07:06:58
【问题描述】:

在我的应用程序中。我必须使用 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>

如果我不传递任何参数,那么我也会得到相同的响应。使用此参数,我可以使用浏览器在线注册新用户。

我认为网络服务器无法获取我的参数,因为参数名称的任何更改都不会影响响应。

【问题讨论】:

  • 你能告诉我你的url需要多少参数吗?
  • url 需要四个参数,我通过了所有四个参数。
  • 你能把你的网址贴出来吗?
  • 使用代码中显示的四个参数,我可以从 Web 服务器的浏览器客户端 api 注册用户

标签: android http parameters http-post


【解决方案1】:

使用的库: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.
            }
        });
}
};

【讨论】:

  • 请不要跨问题重复您的答案。如果问题是重复的,请留下指向另一个问题的评论(或者如果您有足够的声誉,请标记该问题)。如果问题不同,请针对每个问题定制您的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-10
  • 2016-08-07
  • 2017-11-18
相关资源
最近更新 更多