陌简曦东 2015-08-08 08:25 采纳率: 0%
浏览 4084
已采纳

Android中的xutils框架在访问服务器时如何携带json数据

xutils框架的httputils在访问服务器时如何像HttpURLConnection一样携带json数据
HttpURLConnection携带数据的案例,使用put请求:

    URL url;
        HttpURLConnection connection = null;
        int flag = 0;
        try {
            String userIpAddress = "http://" + "124.207.158.74" + ":" + "60022"
                    + "/";
            url = new URL(userIpAddress + APIurl);
            connection = (HttpURLConnection) url.openConnection();
            // 连接主机超时
            connection.setConnectTimeout(100000); // 2015.4.10 add by gaosi
            // 设置从主机读取数据超时
            connection.setReadTimeout(100000);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestMethod("PUT");
            connection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            connection.setRequestProperty("Charset", "utf-8");
            connection.setUseCaches(false);
            JSONObject obj = new JSONObject();
            try {
                obj.put("Username", user.getUsername());
                obj.put("Password", user.getPassword());
                obj.put("Logindevice", user.getLogindevice());
                obj.put("System", user.getSystem());
            } catch (JSONException e) {
                flag = 5;
                return flag;
            }
            // obj.put("Appkey", user.getKey());
            // Log.v("Login", user.getKey());
            DataOutputStream out = new DataOutputStream(
                    connection.getOutputStream());
            out.writeBytes(obj.toString());
            out.flush();
            out.close();

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥15 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取
  • ¥15 sqlserver执行存储过程报错
  • ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
  • ¥15 浮窗和全屏应用同时存在,全屏应用输入法无法弹出
  • ¥100 matlab2009 32位一直初始化
  • ¥15 Expected type 'str | PathLike[str]…… bytes' instead
  • ¥15 三极管电路求解,已知电阻电压和三级关放大倍数