使用json

时间:2016-09-27 18:20:10

标签: android json http-post

我试着用Postman发布我的api并且一切都很好。我也在我的Android应用程序上使用相同的凭据。

唯一必需的元素是:标题:"X-Requested-With", "XMLHttpRequest",并且在正文form-data中:email:value& password:valueSresponse字符串为'invalid-credentials'。我正在使用JSON,POST吗?

这是我的post方法:

  public String requestLogin(final String username, final String password) {
    makeNewURL("login");
    Thread thread= new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                HttpPost post = new HttpPost(url);
                post.addHeader("X-Requested-With", "XMLHttpRequest");
                json.accumulate("email", username);
                json.accumulate("password", password);
                StringEntity params = null;
                params = new StringEntity(json.toString());
                post.setEntity(params);
                HttpResponse response = httpClient.execute(post);
                HttpEntity entity = response.getEntity();
                Sresponse="   "+ EntityUtils.toString(entity);

            }
            catch(Exception x){
                Sresponse=x.getMessage();
            }


        }
    });
    thread.start();

    return Sresponse;
}

0 个答案:

没有答案