【问题标题】:In Android how to post data to webservice which is created in WCF?在 Android 中,如何将数据发布到在 WCF 中创建的 web 服务?
【发布时间】:2016-05-13 09:03:15
【问题描述】:

我是使用 WCF 框架开发的 C# webservice 的新手。我必须在 URL 中发布数据。我的 URL 类似于 http://www.example.com/abc/DGLC.svc/login,我必须使用 post 方法传递数据。参数格式如下。

{

“用户名”:“管理员”,

“密码”:“abcd1234”,

“DiviceType”:“Windows”,

"UniqueID": "deviceidneedtopasshere"

}

请帮助我,如何实现这种 WS。提前致谢。

【问题讨论】:

  • 显示您尝试过的内容?
  • @VishalMokal 是的,它是一个 SOAP 调用

标签: c# android json web-services wcf


【解决方案1】:

创建一个代表 json 字段的类。并在您的网络服务中将其传递给方法参数。并在你的后台线程(asyncTask)中运行这个方法

  public static String postAPIResponse(String url, String data) {

        HttpURLConnection con = null;
        InputStream inputStream;
        StringBuffer responses = null;
        try {
            URL urlObject = new URL(url);
            con = (HttpURLConnection) (urlObject.openConnection());

            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            con.setRequestMethod("POST");
            con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            con.setRequestProperty("Content-Length", Integer.toString(data.getBytes().length));
            con.setRequestProperty("Content-Language", "en-US");
            if (Cookie.getCookie() != null)
                con.addRequestProperty("Cookie", Cookie.getCookie());

            con.setUseCaches(false);
            con.setDoInput(true);
            con.setDoOutput(true);

            //Send request
            DataOutputStream wr = new DataOutputStream(con.getOutputStream());
            wr.writeBytes(data);
            wr.flush();
            wr.close();

            //Get Response
            if (con.getResponseCode() == 200) {


                InputStream is = con.getInputStream();
                BufferedReader rd = new BufferedReader(new InputStreamReader(is));
                String line;
                responses = new StringBuffer();
                while ((line = rd.readLine()) != null) {
                    responses.append(line);
                }
                rd.close();

            } else {

                inputStream = new BufferedInputStream(con.getErrorStream());
                return convertInputStreamToString(inputStream);
            }


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            assert con != null;
            con.disconnect();
        }
        return responses != null ? responses.toString() : "";
    }


static public String convertInputStreamToString(InputStream inputStream) throws IOException {
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    String line;
    String result = "";
    while ((line = bufferedReader.readLine()) != null) {
        result += line;
    }
        /* Close Stream */
    inputStream.close();
    return result;
}

其中数据是 json 对象字符串 new JsonObject.accumulate()... 等与您的服务对象映射

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    相关资源
    最近更新 更多