【问题标题】:Passing JSON obj as parameter in webservice - returns "Bad Request" response在 Web 服务中将 JSON 对象作为参数传递 - 返回“错误请求”响应
【发布时间】:2016-03-02 03:50:50
【问题描述】:

Android 新手,我需要在我的网络服务调用中使用以下参数。我知道参数实际上是 JSON 对象。

以下代码在应返回登录用户信息时返回带有“标题:错误请求”的 XML。 logcat 将值显示为 --> json: {"Query":"com.androidatc.customviewindrawer.Query@f1eb09f","includeUserMiscInfo":true} 表示我的参数不正确。如何正确通过?

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();
    }

查询.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;
    }




}

感谢任何建议。非常感谢。

【问题讨论】:

  • 我已经更新了我的答案尝试这样。但是不要复制和粘贴,因为我是直接在编辑器中写的。

标签: android json web-services


【解决方案1】:

作为您的 Json 类型,您可能必须这样做,

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

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

我想建议你也尝试替换这条线,

json.put("includeUserMiscInfo", true);

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

【讨论】:

  • 这成功了!非常感谢您的帮助,非常感谢。被困了3天。
【解决方案2】:

我认为您在为 HTTPPOST 添加实体时有些困惑。 你需要发送一个 JSONArray 对象,但实际上你发送的是一个 JSON 对象。 请看图:http://prntscr.com/aa49cj

【讨论】:

  • 也许你是对的,我该如何传递 JSONArray 对象?
【解决方案3】:

原因是您没有传递密钥,只是传递值。应该是

somekey = your JSON

通过使用 somekey,您可以从 Web API 访问您的 JSON

【讨论】:

  • 什么是somekey?你能进一步解释一下吗。 tks.
  • somekey 是任意键,数据应该以键值对的形式发送,somekey 可以是您选择的任何值
  • 在代码中也添加了 Query.java。请看看这是不是你的意思?
  • 你没有明白我的意思。数据应该像userName=Dep, field=android 一样发送。以这种方式,它应该在键值对中
  • 我明白你的意思。 logcat 将值显示为 --> json: {"Query":"com.androidatc.customviewindrawer.Query@f1eb09f","includeUserMiscInfo":true} 表示我的参数不正确。但是如何正确通过呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-20
  • 2020-09-12
  • 2023-03-05
  • 2020-01-28
  • 2021-08-13
  • 1970-01-01
相关资源
最近更新 更多