xutils框架在访问网络时如何像HttpURLConnection一样携带json数据

陌简曦东 2015-08-08 04:22:08
HttpURLConnection携带数据代码:
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();
...全文
238 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
gao_chun 2015-08-08
  • 打赏
  • 举报
回复
一般会有类似addParams的方法,携带访问参数。

80,353

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧