java.net.SocketException: Connection reset 解決方式

public class Test {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Test test = new Test(); 
    test.myRun();

}

    private static void myRun(){
        List<Map<String, Object>> etfList= getETFName_db();
        //getETFDebtInfo(etfList,"ETFBasicsINfo",1);//添加ETF基础数据(Name ,类型,基金家族) 已經添加完成
        String url=null;
        List<Map<String, Object>> etfErrorList=new ArrayList<Map<String ,Object>>();
        int count=0;//
        for(int i=0;i<etfList.size();i++){                
            int num=0;                                         
            String chart=etfList.get(i).get("Ticker").toString();
            //https://query1.finance.yahoo.com/v7/finance/chart/"+chart+"?range=10y&interval=1d&indicators=quote"
            //+ "&includeTimestamps=true&includePrePost=false&corsDomain=finance.yahoo.com"
            try {
                url="https://query1.finance.yahoo.com/v7/finance/chart/"+chart+"?range=10y&interval=1d&indicators=quote"
                        + "&includeTimestamps=true&includePrePost=false&corsDomain=finance.yahoo.com";
                while(Run(url)==0){
                    num++;
                    if(num<3){
                        continue;
                    }else {
                        Map<String, Object>ETFMap =new HashMap<>();
                        ETFMap.put("ETF", chart);
                        etfErrorList.add(ETFMap);
                        break;
                    }
                }
            } catch (Exception e) {
                // TODO: handle exception
                if(num<3){
                    count++;
                }
                System.out.println(chart+"出錯");
            }
        }
        getETFDebtInfo(etfErrorList,"ETFBasicsINfo",2);
        if(etfErrorList.size()>0){
            System.out.println("添加完成!共"+count+"条!");
            System.out.println("失败"+etfErrorList.size()+"条!");
            System.out.println("失败信息:"+etfErrorList);
        }else{
            System.out.println("全部添加完成!共"+count+"条!");
        }
    }


    @SuppressWarnings("unchecked")
    private static  int Run(String url){
        Map<String,Object> htmlMap=new HashMap<>();//1
        Map<String,Object> chartMap=new HashMap<>();//2
        List<Map<String, Object>> mapList=new ArrayList<Map<String,Object>>();//3
        Map<String,Object>map4=new HashMap<>();//4
        cUrl c=new cUrl();
        String html=null;
        try {
            html=c.httpGet(url);//得到一个html 页面元素
            if(html == null || html.length() < 10){
                return 0;
            }
        } catch (Exception e) {
            // TODO: handle exception

        }

        htmlMap= (Map<String, Object>) JSONObject.parse(html);//把该页面元素转成JSON对象  存如Map
        for (String charkey : htmlMap.keySet()){
            chartMap=(Map<String, Object>) htmlMap.get(charkey);
            for (String chartkey : chartMap.keySet()) {//2

                if(chartMap.get(chartkey)!=null){
                mapList=(List<Map<String, Object>>) chartMap.get(chartkey);
                map4=(Map<String, Object>) mapList.get(0).get("meta");

                getETFDebtInfo(mapList,map4.get("symbol").toString(),0);

                }
            }

        }
        return 1;
    }


    private static void getETFDebtInfo(List<Map<String, Object>> getETFDebtInfoList,String ETFName,int num) {
                String RobomongDBName=null;
        switch (num) {
        case 0:
            RobomongDBName="ETFDebtInfo";
            break;
        case 1:
            RobomongDBName="ETFBasicsINfo";
            break;
        case 2:
            RobomongDBName="ETFErrorLog";
            break;
        default:
            break;
        }
        List<Map<String, Object>> mfo = getETFDebtInfoList;
        Map<String, Object> kMap = new HashMap<>();
        kMap.put(ETFName, mfo);
        String kJson = JSON.toJSONString(kMap);
        MGDB mgdb = new MGDB();
        mgdb.DBEnd();
        mgdb.SetCollection(RobomongDBName);
        Map<String, Object> wMap = new HashMap<>();
        Map<String, Object> eMap = new HashMap<>();
        eMap.put(mgdb.exists, 1);
        wMap.put(RobomongDBName, eMap);
        String wjson = JSON.toJSONString(wMap);
        mgdb.JsonWhere(wjson);
        mgdb.Delete();
        mgdb.Insert(kJson);
        mgdb.Close();       
        System.out.println("添加"+RobomongDBName+"数据 ok!");
    }

3个回答

每次运行都只能加载数据到50 到150条数据 就开始报错。这是什么问题?代码在楼上。求解答。

Connection reset ,连接重置,我上次碰到,也因为使用socket时,多个客户端,有断开服务器的时,就会Connection reset

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问