JAVA_JSON_example
package cn.kjxy.JSON; import java.util.List; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; /**
* 解析网络数据
* path:"http://interfacev5.vivame.cn/x1-interface-v5/json/newdatalist.json?platform=android&installversion=5.2.0&channelno=OPPOA2320480100&mid=0a0fec95ea3364f43798a33fc3616780&uid=3348521&sid=a27ef935-f451-46ad-962b-0a48c47d47ae&type=-1&id=-1&category=-1&ot=0&nt=0"
* 步骤:1.获取路径
* 2.通过HttpClient从该网络路径获取json数据
* 3.通过bejson工具进行json校验
* 4.创建相关对象或数组进行json解析
* @author Administrator
* 注意: 需导入HttpClient的三个jar包及JSON的jar包
*/ class Item{
private int stypeid;//": 3,
private String stypename;//: "图集",
private String title;//": "暴雨致赣南5.3万人受灾",
private String img;//": "http://stcv5.vivame.cn/pmsV5/upload/file/20160323/f116d696-c777-43a0-a709-d044dec48900.jpg",
public int getStypeid() {
return stypeid;
}
public void setStypeid(int stypeid) {
this.stypeid = stypeid;
}
public String getStypename() {
return stypename;
}
public void setStypename(String stypename) {
this.stypename = stypename;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
@Override
public String toString() {
return "Items [stypeid=" + stypeid + ", stypename=" + stypename
+ ", title=" + title + ", img=" + img + "]";
} }
public class Demo2 {
public static void main(String[] args) {
String path = "http://interfacev5.vivame.cn/x1-interface-v5/json/newdatalist.json?platform=android&installversion=5.2.0&channelno=OPPOA2320480100&mid=0a0fec95ea3364f43798a33fc3616780&uid=3348521&sid=a27ef935-f451-46ad-962b-0a48c47d47ae&type=-1&id=-1&category=-1&ot=0&nt=0";
//应用自制工具类解析JSON数据
String json = HttpHelpers.getResourceByInternet(path);
parserJSON(json);
} private static void parserJSON(String json) {
// TODO Auto-generated method stub
try {
//创建JSON对象
JSONObject jsonObject = new JSONObject(json);
JSONObject data = jsonObject.getJSONObject("data");
JSONArray feedList = data.getJSONArray("feedlist");
for (int i = 0; i < feedList.length(); i++) {
JSONObject jsonObject2 = feedList.getJSONObject(i);
JSONArray items = jsonObject2.getJSONArray("items");
for (int j = 0; j < items.length(); j++) {
JSONObject jsonObject3 = items.getJSONObject(j);
Item item = new Item();
item.setStypeid(jsonObject3.getInt("stypeid"));
item.setStypename(jsonObject3.getString("stypename"));
item.setTitle(jsonObject3.getString("title"));
item.setImg(jsonObject3.getString("img"));
System.out.println(item);
} }
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package cn.kjxy.JSON; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils; /**
* 请求网络的工具类
*
* @author Administrator
*
*/
public class HttpHelpers { /**
* 下载图片 保存到byte类型的数组中
*
* @param path
* 地址
* @return byte[]
*/
public static byte[] downLoadImg(String path) {
BufferedInputStream bis = null;
try {
// 1,创建HttpClient对象 Android6.0之前可以使用
HttpClient httpClient = new DefaultHttpClient();
// 2,创建请求对象+指定地址
HttpGet httpGet = new HttpGet(path);
// 3,执行请求 获得HttpResponse对象
HttpResponse response = httpClient.execute(httpGet);
// 4,获得响应码
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
// 5,得到响应的HttpEntity对象
HttpEntity responseEntity = response.getEntity();
// 方法一
// bis = new BufferedInputStream(responseEntity.getContent());
// byte b[] = toByteArray(bis);
// return b; // 方法二
return EntityUtils.toByteArray(responseEntity); } } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} return null; } /**
* 把图片下载到本地磁盘
*
* @param path
*/
public static void downLoadImgToLocal(String path) {
BufferedInputStream bis = null;
BufferedOutputStream boStream = null;
try {
// 1,创建HttpClient对象 Android6.0之前可以使用
HttpClient httpClient = new DefaultHttpClient();
// 2,创建请求对象+指定地址
HttpGet httpGet = new HttpGet(path);
// 3,执行请求 获得HttpResponse对象
HttpResponse response = httpClient.execute(httpGet);
// 4,获得响应码
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
// 5,得到响应的HttpEntity对象
HttpEntity responseEntity = response.getEntity();
// 方法一
// bis = new BufferedInputStream(responseEntity.getContent());
// byte b[] = toByteArray(bis); // 方法二
byte b[] = EntityUtils.toByteArray(responseEntity);
String endsWith = path.substring(path.lastIndexOf("."));
boStream = new BufferedOutputStream(new FileOutputStream((int) (Math.random() * 100) + endsWith));
boStream.write(b); } } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (boStream != null) {
try {
boStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} } /**
* 从互联网获取文本 json xml
*
* @param path
* 地址
* @return 获取到的文本数据
*/ public static String getResourceByInternet(String path) {
BufferedReader bReader = null;
try {
// 1,创建HttpClient对象 Android6.0之前可以使用
HttpClient httpClient = new DefaultHttpClient();
// 2,创建请求对象+指定地址
HttpGet httpGet = new HttpGet(path);
// 3,执行请求 获得HttpResponse对象
HttpResponse response = httpClient.execute(httpGet);
// 4,获得响应码
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
// 得到HttpEntity对象
HttpEntity responseEntity = response.getEntity();
// 方法一
// bReader = new BufferedReader(new
// InputStreamReader(responseEntity.getContent()));
// StringBuilder sbBuilder = new StringBuilder();
// String line = null;
// while ((line = bReader.readLine()) != null) {
// sbBuilder.append(line);
// }
//
// return sbBuilder.toString(); // 方法二
return EntityUtils.toString(responseEntity); } } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (bReader != null) {
try {
bReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} return null; } public static byte[] toByteArray(BufferedInputStream bufferedInputStream) {
byte b[] = new byte[1024 * 1024];
int len = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
while ((len = bufferedInputStream.read(b)) != -1) {
baos.write(b, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return baos.toByteArray(); } public static String upLoadData(String path, Map<String, String> map) {
BufferedReader bReader = null;
try {
// 1,创建HttpClient对象 Android6.0之前可以使用
HttpClient httpClient = new DefaultHttpClient();
// 2,创建请求对象+指定地址
HttpPost httpPost = new HttpPost(path);
// 设置用于发送到服务端的参数
List<NameValuePair> list = new ArrayList<NameValuePair>(); for (String string : map.keySet()) {
list.add(new BasicNameValuePair(string, map.get(string)));
}
HttpEntity requestEntity = new UrlEncodedFormEntity(list, "gbk");
httpPost.setEntity(requestEntity); // 3,执行请求 获得HttpResponse对象
HttpResponse response = httpClient.execute(httpPost);
// 4,获得响应码
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
// 得到HttpEntity对象
HttpEntity responseEntity = response.getEntity();
// 方法一
// bReader = new BufferedReader(new
// InputStreamReader(responseEntity.getContent()));
// StringBuilder sbBuilder = new StringBuilder();
// String line = null;
// while ((line = bReader.readLine()) != null) {
// sbBuilder.append(line);
// }
//
// return sbBuilder.toString(); // 方法二
return EntityUtils.toString(responseEntity, "gbk"); } } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (bReader != null) {
try {
bReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} return null; } public static void main(String[] args) {
byte b[] = downLoadImg("http://images.china.cn/attachement/jpg/site1000/20140313/844bf52c7d7c148b8abc05.jpg");
BufferedOutputStream bufferedOutputStream;
try {
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("ceo.jpg"));
// bufferedOutputStream.write(b); } catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // downLoadImgToLocal("http://images.china.cn/attachement/jpg/site1000/20140313/844bf52c7d7c148b8abc05.jpg"); // System.out.println(getResourceByInternet("http://www.doukantv.com/api/hot/?type=movie&cli=ipad&sys_ver=7.1.1&ver=2.1"));
Map<String, String> map = new HashMap<String, String>();
map.put("uname", "张三");
map.put("pwd", "admin");
System.out.println(upLoadData("http://172.20.136.5:8080/2016_05_27_server/login", map)); } }
JAVA_JSON_example的更多相关文章
随机推荐
- 与非CCR代码互操作
导读:CCR可以轻松的承载STA组件或者与它互操作:组件应该创建一个只有一个线程的CCR Dispatcher实例,并且在Dispatcher的构造函数中指定线程套间策略.DispatcherQueu ...
- 6、四大组件之二-Service初步
一.什么是Service 有些用时比较长得操作我们希望他在后台运行 ,不耽误我们当前的操作 . 这就引入了Service概念 . 常见的比如:访问网络 , 文件IO操作 , 大数据的数据库任务,播放音 ...
- IOS 多线程 NSOperation GCD
1.NSInvocationOperation NSInvocationOperation * op; NSOperationQueue * que = [[NSOperationQueuealloc ...
- js会飞的li标签
当点击左边的li标签的时候,这边的li标签飞到右边去,点击右边的li标签飞到左边来,并且有顺序 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...
- 自己常用的js方法
$(function(){ tabview("#zcfw_list1"); tabview("#zcfw_list2"); tabview("#zcf ...
- 相当管用了 mstha插件
http://www.i-magical.com/2010/04/feizhuliu-kill-virus-mshta-exe/ 非主流杀毒 – mshta.exe Vincent | Apr 23, ...
- 恒天云技术分享系列2 - vlan管理GUI开发
恒天云:http://www.hengtianyun.com/download-show-id-10.html 在OpenStack G版本中quantum网络模式下,horizon提供了基于quan ...
- 44 个 JavaScript 变态题解析
当初笔者做这套题的时候不仅怀疑智商, 连人生都开始怀疑了…. 不过, 对于基础知识的理解是深入编程的前提. 让我们一起来看看这些变态题到底变态不变态吧! 第1题 ["1", &qu ...
- Java网络编程(客户端和服务端原理)
运行下面的程序,浏览器端输入自己主机的IP地址+端口号(8888),会看到服务器返回的数据内容,Eclipse控制台会打印显示收到的信息, 通过我们自定义的服务器,可以看到浏览器端向服务器发送的请求信 ...
- jquery validation 简单验证手机号码
js代码 // 手机号码验证 jQuery.validator.addMethod("isMobile", function(value, element) { var lengt ...