如何将webservice参数作为JSON对象传递?

时间:2016-03-01 05:22:53

标签: android json web-services

Android新手,webservice调用返回状态码400.我怀疑这是由于传递参数的方式不正确。我需要将它们作为JSON对象传递,但不知道我该怎么做?

下面应该是参数并且工作正常。

enter image description here

在我的Android代码中,它显示状态代码400。

(更新后的代码如下)

Table : Employee
ID    salary      emp_name
1     400         A
2     800         B
3     300         C
4     400         D
4     400         C

*** Mysql Query: ***
SELECT * FROM employee ORDER by salary DESC LIMIT 1,2

我也尝试过如下JSON对象。

    RequestParams params = new RequestParams();


    Map<String, String> map = new HashMap<String, String>(); 
map.put("login", "WT"); map.put("password", "03"); 
params.put("query", map); params.put("includeUserMiscInfo", "true");

client.post("http://XXXX/SDService_SAFTI/ServiceSD.svc/LoginUser",params,new AsyncHttpResponseHandler() {

但它返回 - 请求错误xml

欢迎任何建议。非常感谢提前。

2 个答案:

答案 0 :(得分:0)

您确定自己的网址http://192.168.0.102/DService/ServiceSD.svc/LoginUser?Query=login=WT&password=03&includeUserMiscInfo=true是否正确?

答案 1 :(得分:0)

您可以使用以下方法来呼叫您的网络服务:

public void makeHTTPCall() {

        RequestParams params = new RequestParams();
        String query = "[login=WT&password=03]"
        params.put("query", query);
        params.put("includeUserMiscInfo", "true");

        prgDialog.setMessage(getResources().getString(R.string.please));
        AsyncHttpClient client = new AsyncHttpClient();

        client.post("http://192.168.0.102/SDService_SAFTI/ServiceSD.svc/LoginUser",
                params, new AsyncHttpResponseHandler() {

            @Override
            public void onSuccess(String response) {
                // Hide Progress Dialog
                prgDialog.hide();       

// do your code if result is success

            }


            @Override
            public void onFailure(int statusCode, Throwable error,
                    String content) {

                prgDialog.hide();

                if (statusCode == 404) {
                    Toast.makeText(getApplicationContext(),
                            "Requested resource not found",
                            Toast.LENGTH_LONG).show();
                }

                else if (statusCode == 500) {
                    Toast.makeText(getApplicationContext(),
                            "Something went wrong at server end",
                            Toast.LENGTH_LONG).show();
                }

                else {
                    Toast.makeText(
                            getApplicationContext(),
                            getResources().getString(R.string.some_error_occured)
                            , Toast.LENGTH_LONG)
                            .show();
                }
            }
        });
    }

希望这会对你有所帮助。