Java解析网络数(Json)运用CloseableHttpClient

来源:互联网 发布:仿美文网整站源码 编辑:程序博客网 时间:2024/06/13 13:46

最近做用Java网络爬取数据的部分,发现在使用Apache的httpclient的时候,发现Idea提示DefaultHttpClient等常用的类已经不推荐使用了。现在运用 CloseableHttpClient
和 CloseableHttpResponse类

开发环境:Maven3.3.9

   IDEA2016.2.1

import net.sf.json.JSONArray;import net.sf.json.JSONObject;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClientBuilder;import org.apache.http.util.EntityUtils;import java.io.IOException;import java.util.ArrayList;import java.util.List;/** * Created by 清水66 on 2017/8/28. */public class JsonGet {    public static void main(String[] args){        //创建HttpClientBuilder        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();        //HttpClient        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();        HttpGet httpGet = new HttpGet("http://android.myapp.com/myapp/app/comment.htm?apkName=com.youcash.ZYWallet&apkCode=157&p=1&fresh=0.02709822286851704&contextData=");        System.out.println(httpGet.getRequestLine());        try {            //执行get请求            HttpResponse httpResponse = closeableHttpClient.execute(httpGet);            //获取响应消息实体            HttpEntity entity = httpResponse.getEntity();            //响应状态            System.out.println("status:" + httpResponse.getStatusLine());            //判断响应实体是否为空            if (entity != null) {                System.out.println("contentEncoding:" + entity.getContentEncoding());                parseJsonGet(EntityUtils.toString(entity));            }        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                //关闭流并释放资源                closeableHttpClient.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }    //解析Json数据    public static void parseJsonGet(String jsonString){        JSONObject jsonObject = JSONObject.fromObject(jsonString);        //AppCommentsData类,存放评论的作者、评论内容等        List<AppCommentsData> list = new ArrayList<AppCommentsData>();        JSONObject obj= jsonObject.getJSONObject("obj");        //商品评论信息        JSONArray jsonArray = obj.getJSONArray("commentDetails");        for(int i =0;i<jsonArray.size();i++){            AppCommentsData appCommentsData = new AppCommentsData();            appCommentsData.setApp_id(1);            appCommentsData.setStore_id(1011);            //评论作者            appCommentsData.setAuthor(jsonArray.getJSONObject(i).getString("nickName"));            //商品评论内容            appCommentsData.setData(jsonArray.getJSONObject(i).getString("content"));            //评分            appCommentsData.setScore(jsonArray.getJSONObject(i).getInt("score"));            list.add(appCommentsData);        }        System.out.println("输出appCommentsData//");        for (int i=0;i<list.size();i++){            AppCommentsData appCommentsData = list.get(i);            System.out.println("name: ="+appCommentsData.getAuthor());            System.out.println("content: ="+appCommentsData.getData());            System.out.println("score: ="+appCommentsData.getScore());        }    }}
类AppCommentsData如下:

/** * Created by 清水66 on 2017/8/22. */public class AppCommentsData {    int id;    int app_id;//app的id    int store_id;//应用商店id    String author;//评论人    int score;//评分    String  data;//评论内容    //生成getter.setter方法}
注意;1)在获取评论内容时,并把评论内容插入MySQL数据库时,由于评论内容有表情符号会报Incorrect string value: '\xF0\x9F\x98\xAD",...' 错误,

2)网络上建议把UTF-8改为utf8mb4,但是尝试了几个帖子上的操作没有成功,

        3)本人把评论内容的表情去掉插入数据库中了

String content = jsonArray.getJSONObject(i).getString("content").replaceAll("[\\x{10000}-\\x{10FFFF}]", "");            appCommentsData.setData(content);


参考:http://blog.csdn.net/leeo1010/article/details/41801509





原创粉丝点击