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();
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。