学习json post请求和json解析
2015-02-09 13:44
375 查看
post请求
/**
* 发送HttpPost请求
*
* @param strURL
* 服务地址
* @param params
* json字符串,例如: "{ \"id\":\"12345\" }" ;其中属性名必须带双引号<br/>
* @return 成功:返回json字符串<br/>
*/
public static String post(String strURL, String params) {
System.out.println(strURL);
System.out.println(params);
try {
URL url = new URL(strURL);// 创建连接
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestMethod("POST"); // 设置请求方式
connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
connection.connect();
OutputStreamWriter out = new OutputStreamWriter(
connection.getOutputStream(), "UTF-8"); // utf-8编码
out.append(params);
out.flush();
out.close();
// 读取响应
int length = (int) connection.getContentLength();// 获取长度
InputStream is = connection.getInputStream();
if (length != -1) {
byte[] data = new byte[length];
byte[] temp = new byte[512];
int readLen = 0;
int destPos = 0;
while ((readLen = is.read(temp)) > 0) {
System.arraycopy(temp, 0, data, destPos, readLen);
destPos += readLen;
}
String result = new String(data, "UTF-8"); // utf-8编码
System.out.println(result);
return result;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "error"; // 自定义错误信息
}
json解析(不通用根据实际情况修改)
public ArrayList<Tab_Activity> jsonToArray(String jsonStr){
JSONObject dataJson;
Tab_Activity mTab_Activity = null;
ArrayList<Tab_Activity> mActivityList = new ArrayList<Tab_Activity>();
try {
dataJson = new JSONObject(jsonStr);
// JSONObject response = dataJson.getJSONObject("Output");
// 获得数组个数
int count = dataJson.getInt("count");
Log.i("tjj", "count = "+count);
JSONArray data = dataJson.getJSONArray("Activities");
Log.i("tjj", "data = "+data.length());
// 循环创建json对象
JSONObject[] info = new JSONObject[count];
for(int i = 0; i < count; i++){
info[i] = data.getJSONObject(i);
Log.i("tjj", "info["+i+"] = "+info[i].length());
// 将获得的信息记录
mTab_Activity = new Tab_Activity();
mTab_Activity.setId(info[i].getInt("id"));
Log.d("tjj", mTab_Activity.getId()+"");
mTab_Activity.setImage_url(info[i].getString("imageUrl"));
Log.d("tjj", mTab_Activity.getImage_url());
mTab_Activity.setAction(info[i].getString("action"));
Log.d("tjj", mTab_Activity.getAction());
mTab_Activity.setAct_des(info[i].getString("actDes"));
Log.d("tjj", mTab_Activity.getAct_des());
mTab_Activity.setAct_number(info[i].getInt("actNumber"));
Log.d("tjj", mTab_Activity.getAct_number()+"");
mTab_Activity.setAct_type(info[i].getInt("actType"));
Log.d("tjj", mTab_Activity.getAct_type()+"");
// mTab_Activity.setAct_start_time(null);
// mTab_Activity.setAct_end_time(info[i].ge);
mActivityList.add(mTab_Activity);
}
return mActivityList;
// String actCode=info[0].getString("province");
// String city=info[0].getString("city");
// String district=info[0].getString("district");
// String address=info[0].getString("address");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 发送HttpPost请求
*
* @param strURL
* 服务地址
* @param params
* json字符串,例如: "{ \"id\":\"12345\" }" ;其中属性名必须带双引号<br/>
* @return 成功:返回json字符串<br/>
*/
public static String post(String strURL, String params) {
System.out.println(strURL);
System.out.println(params);
try {
URL url = new URL(strURL);// 创建连接
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestMethod("POST"); // 设置请求方式
connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
connection.connect();
OutputStreamWriter out = new OutputStreamWriter(
connection.getOutputStream(), "UTF-8"); // utf-8编码
out.append(params);
out.flush();
out.close();
// 读取响应
int length = (int) connection.getContentLength();// 获取长度
InputStream is = connection.getInputStream();
if (length != -1) {
byte[] data = new byte[length];
byte[] temp = new byte[512];
int readLen = 0;
int destPos = 0;
while ((readLen = is.read(temp)) > 0) {
System.arraycopy(temp, 0, data, destPos, readLen);
destPos += readLen;
}
String result = new String(data, "UTF-8"); // utf-8编码
System.out.println(result);
return result;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "error"; // 自定义错误信息
}
json解析(不通用根据实际情况修改)
public ArrayList<Tab_Activity> jsonToArray(String jsonStr){
JSONObject dataJson;
Tab_Activity mTab_Activity = null;
ArrayList<Tab_Activity> mActivityList = new ArrayList<Tab_Activity>();
try {
dataJson = new JSONObject(jsonStr);
// JSONObject response = dataJson.getJSONObject("Output");
// 获得数组个数
int count = dataJson.getInt("count");
Log.i("tjj", "count = "+count);
JSONArray data = dataJson.getJSONArray("Activities");
Log.i("tjj", "data = "+data.length());
// 循环创建json对象
JSONObject[] info = new JSONObject[count];
for(int i = 0; i < count; i++){
info[i] = data.getJSONObject(i);
Log.i("tjj", "info["+i+"] = "+info[i].length());
// 将获得的信息记录
mTab_Activity = new Tab_Activity();
mTab_Activity.setId(info[i].getInt("id"));
Log.d("tjj", mTab_Activity.getId()+"");
mTab_Activity.setImage_url(info[i].getString("imageUrl"));
Log.d("tjj", mTab_Activity.getImage_url());
mTab_Activity.setAction(info[i].getString("action"));
Log.d("tjj", mTab_Activity.getAction());
mTab_Activity.setAct_des(info[i].getString("actDes"));
Log.d("tjj", mTab_Activity.getAct_des());
mTab_Activity.setAct_number(info[i].getInt("actNumber"));
Log.d("tjj", mTab_Activity.getAct_number()+"");
mTab_Activity.setAct_type(info[i].getInt("actType"));
Log.d("tjj", mTab_Activity.getAct_type()+"");
// mTab_Activity.setAct_start_time(null);
// mTab_Activity.setAct_end_time(info[i].ge);
mActivityList.add(mTab_Activity);
}
return mActivityList;
// String actCode=info[0].getString("province");
// String city=info[0].getString("city");
// String district=info[0].getString("district");
// String address=info[0].getString("address");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
相关文章推荐
- Netty学习笔记之一(Netty解析简单的Http Post Json 请求)
- Objective-C-使用NSMutableURLRequest发送POST请求,使用NSJSONSerialization解析JSON字符串
- 学习express之实现Json的Post请求
- javaweb使用get与post请求json数据并解析
- Struts2 Action接收POST请求JSON数据及其实现解析
- 异步POST请求解析JSON
- IOS学习笔记,网络请求,json解析
- 【soapUI】解决post请求时,body为json串,服务器无法解析问题
- JAVA学习笔记--线程请求Json数据,并解析绑定到listView
- 网络请求与json数据解析——学习笔记
- WP runtime post 请求, json 解析
- 安卓POST请求+异步获取数据+JSON解析+图片解析
- Android+PHP 使用HttpClient提交POST的请求,使用JSON解析响应
- Android的okhttp的post请求,php返回json数据。以及遇到的okhttp dispatcher问题,和json解析遇到的问题
- http get/post请求及json解析
- android菜鸟学习笔记25----与服务器端交互(二)解析服务端返回的json数据及使用一个开源组件请求服务端数据
- post请求和json解析
- swift 学习笔记1 -- json解析、网络请求、[myClass class]
- Java post请求发送json数据在filter中解析方法
- 如何使用webmagic发送post请求,并解析传回的JSON