传递JSON作为OBJ参数web服务 - 收益"坏请求"响应 [英] Passing JSON obj as parameter in webservice - returns "Bad Request" response

查看:154
本文介绍了传递JSON作为OBJ参数web服务 - 收益"坏请求"响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新到Android,我需要使用下面的参数在我的Web服务调用。据我所知,参数实际上是JSON对象。

New to Android, I need to use below parameters in my webservices call. I understand that parameters are actually JSON objects.

在这里输入的形象描述

下面code返回XML与标题:坏请求时,它应该返回登录的用户信息。 logcat的是显示值 - > JSON:{查询:com.androidatc.customviewindrawer.Query@f1eb09f,includeUserMiscInfo:真正}意味着我的参数是不正确的。如何正确地传递呢?

Below code returns XML with "title: Bad request" when it should return logged in user info. logcat is showing value as --> json: {"Query":"com.androidatc.customviewindrawer.Query@f1eb09f","includeUserMiscInfo":true} means my parameter is not correct. How to pass it correctly?

protected void sendJson(final String email, final String pwd) {
        Thread t = new Thread() {

            public void run() {
                Looper.prepare(); //For Preparing Message Pool for the child Thread
                HttpClient client = new DefaultHttpClient();
                HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
                HttpResponse response;
                JSONObject json = new JSONObject();

                try {
                    HttpPost post = new HttpPost("http://192.168.0.102/SDService_SAFTI/ServiceSD.svc/LoginUser");
                    Query queryObj = new Query();
                    queryObj.setLogin("WT");
                    queryObj.setPassword("3");


                    json.put("Query", queryObj);
//                  json.put("email", email);
//                  json.put("password", pwd);
                    json.put("includeUserMiscInfo", true);


                    StringEntity se = new StringEntity( json.toString());
                    se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                    post.setEntity(se);
                    response = client.execute(post);

                    /*Checking response */
                    if(response!=null){


                        InputStream in = response.getEntity().getContent(); //Get the data in the entity
                         Toast.makeText(getActivity().getApplicationContext(), "Response:" + convertStreamToString(in),Toast.LENGTH_LONG).show();

                    }

                } catch(Exception e) {
                    e.printStackTrace();
//                  getActivity().createDialog("Error", "Cannot Estabilish Connection");
                }

                Looper.loop(); //Loop in the message queue
            }
        };

        t.start();
    }

    private static String convertStreamToString(InputStream is) {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append((line + "\n"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

Query.java

Query.java

public class Query {
    String login;

    public void setPassword(String password) {
        this.password = password;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    String password;

    public String getPassword() {
        return password;
    }

    public String getLogin() {
        return login;
    }




}

任何建议都AP preciated。提前很多感谢。

Any suggestions are appreciated. Many thanks in advance.

推荐答案

由于您的JSON类型,你可能需要做这样的,

As your Json Type, you may have to do like this,

JSONArray array = new JSONArray();
array.put("login=WT&password=3");

json.put("Query", array);
json.put("includeUserMiscInfo", "true");

我想建议你去尝试更换这个太行,

I want to suggest you to try replacing this line too,

json.put("includeUserMiscInfo", true);

json.put("includeUserMiscInfo", "true");

这篇关于传递JSON作为OBJ参数web服务 - 收益"坏请求"响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆