• Socket读取JSONArray字串越界等相关问题


    好长一个json!server就是把一张表的数据直接复制给我了,还是字符串的形式!

    然后,然后就越界了,byte[] readByte = new byte[1024]; dataIn.read(readByte, 1, 1000); ——ArrayIndexOutofBounds!加长即是!

    byte[] readByte = new byte[1024*1024];
    dataIn.read(readByte, 1, 10000);
    

    还有,就是socket读取的中文乱码问题,转换如下(第1行、第7行):

    1 outw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream(), "GBK")), true);
    2 outw.println(MakeQueryStr.makeProjectStr(uidstr));
    3 String queryResult = "";
    4 dataIn = new DataInputStream(s.getInputStream());
    5 byte[] readByte = new byte[1024*1024];
    6 dataIn.read(readByte, 1, 10000);
    7 String readData = new String(readByte, "GBK").trim();

    然后将得到的socket字串保存到JSONArray当中:

    		JSONArray projectJsonArray = null;
    		String soStr = loadProjectSocket(con,uname);
    		String socketStr = soStr.substring(soStr.indexOf("[")); 
    		try {
    			projectJsonArray = new JSONArray(socketStr);
    		} catch (JSONException e) {
    			e.printStackTrace();
    		}
    

     新建实体,将JSONArray数据解析并保存到Arraylist<Entity>当中:

    Entity
     1         if(jsonarr!=null){
    2 ArrayList<PayProject> project=new ArrayList();
    3 int length = jsonarr.length();
    4 for(int i = 0; i < length; i++) {//遍历JSONArray
    5 try {
    6 PayProject proj = new PayProject();
    7 JSONObject oj = jsonarr.getJSONObject(i);
    8 proj.prov_id = oj.getInt("prov_id");
    9 proj.prov_name = oj.getString("prov_name");
    10 proj.memo = oj.getString("memo");
    11 proj.pro_type = oj.getInt("pro_type");
    12 proj.face_value = oj.getDouble("face_value");
    13 proj.product_sn = oj.getInt("product_sn");
    14 proj.opt1_name = oj.getString("opt1_name");
    15 proj.opt1_valuelist = oj.getString("opt1_valuelist");
    16 proj.opt2_name = oj.getString("opt2_name");
    17 proj.opt2_valuelist = oj.getString("opt2_valuelist");
    18 proj.opt3_name = oj.getString("opt3_name");
    19 proj.opt3_valuelist = oj.getString("opt3_valuelist");
    20 project.add(proj);
    21 } catch (JSONException e) {
    22 e.printStackTrace();
    23 }
    24 Log.d(LOG,"projectArray["+Integer.toString(i)+"].size="+project.size());
    25 Log.d(LOG, "project.get("+i+").prov_name"+project.get(i).prov_name);
    26 }
    27 }

    完成上述操作,原socket每行的记录就保存到了Entity当中,遍历ArrayList,就能很方便的读取相关列。

    附-无线程的socket操作:

    Socket
     1     @SuppressWarnings("finally")
    2 public static String loadProjectSocket(Context con, String uidstr) {
    3 PrintWriter outw = null;
    4 DataInputStream dataIn = null;
    5 Socket s = null;
    6 try {
    7 String[] param = CheckServer.getServer(con);
    8 servername = param[0];
    9 s = new Socket(servername, 53303);
    10 outw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream(), "GBK")), true);
    11 outw.println(MakeQueryStr.makeProjectStr(uidstr));
    12 String queryResult = "";
    13 dataIn = new DataInputStream(s.getInputStream());
    14 byte[] readByte = new byte[1024*1024];
    15 dataIn.read(readByte, 1, 10000);
    16 String readData = new String(readByte, "GBK").trim();
    17 queryResult = readData;
    18 boolean sucif = queryResult.contains("prov_id");
    19 if (sucif) {
    20 Log.d(TAG, "loadProjectSocket_success");
    21
    22 oredersocketStr = queryResult ;
    23 } else {
    24 Log.d(TAG, "loadProjectSocket_success");
    25 oredersocketStr = "-1";
    26 }
    27 } catch (Exception e) {// 捕获异常
    28 e.printStackTrace();// 打印异常
    29 System.out.println("连接服务器失败");
    30 } finally {// 使用finally保证之后的语句一定被执行
    31 try {
    32 if (dataIn != null) {
    33 dataIn.close();
    34 dataIn = null;
    35 }
    36 } catch (Exception e) {
    37 e.printStackTrace();
    38 }
    39 try {
    40 if (outw != null) {
    41 outw.close();
    42 outw = null;
    43 }
    44 } catch (Exception e) {
    45 e.printStackTrace();
    46 }
    47 try {
    48 if (s != null) {
    49 s.close();
    50 s = null;
    51 Log.d(TAG,
    52 "......Socket finished.....");
    53 }
    54 } catch (Exception e) {
    55 e.printStackTrace();
    56 }
    57 return oredersocketStr;
    58 }
    59
    60 }



    添加:循环接收socket字段:

    socket 循环接收
     1     @SuppressWarnings("finally")
     2     public static String Socket() {
     3         PrintWriter outw = null;
     4         DataInputStream dataIn = null;//BufferedInputStream
     5         Socket s = null;
     6         try {
     7             System.out.println("......Socket Starting.....");
     8             s = new Socket("192.168.0.119",53303);
     9             outw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
    10                     s.getOutputStream(), "GBK")), true);
    11             String str = ListUnReadMsgStr("szmaster","12345678");
    12             outw.println(str);
    13             System.out.println("服务器:" + s.getRemoteSocketAddress());
    14             dataIn = new DataInputStream(s.getInputStream());
    15             byte[] readByte = new byte[1024];
    16             int i = 0;
    17             while ((i = dataIn.read(readByte, 1, 1000)) !=-1) {
    18                 readData += new String(readByte,"gbk").trim();
    19                 System.out.println(i+":"+readData);
    20                 if(i<1000)break;
    21             }
    22             queryResultstr = readData;
    23             int mesleg = queryResultstr.getBytes().length;
    24             System.out
    25             .println("loadPaymentSocketresult:【mesleg = "
    26                     + mesleg + "】"+queryResultstr);
    27         } catch (Exception e) {// 捕获异常
    28             queryResultstr = "error";
    29             e.printStackTrace();
    30             System.out.println("连接服务器失败");
    31         } finally {// 使用finally保证之后的语句一定被执行
    32 
    33             try {
    34                 if (dataIn != null) {
    35                     dataIn.close();
    36                     dataIn = null;
    37                 }
    38             } catch (Exception e) {
    39                 e.printStackTrace();
    40             }
    41             try {
    42                 if (outw != null) {
    43                     outw.close();
    44                     outw = null;
    45                 }
    46             } catch (Exception e) {
    47                 e.printStackTrace();
    48             }
    49             try {
    50                 if (s != null) {
    51                     s.close();
    52                     s = null;
    53                     System.out.println("......Socket finished.....");
    54                 }
    55             } catch (Exception e) {
    56                 e.printStackTrace();
    57             }
    58             return queryResultstr;
    59         }
    60 
    61     }
  • 相关阅读:
    字典的增删改查
    列表的增删改查 and 元祖
    基础数据类型
    python初始1
    python初始
    android APP 中微信分享功能实现 的总结
    android ADT 版本过低的解决办法
    android 布局: LinearLayout如何使TextView中的内容居中显示
    android获取项目下的一张图片的绝对路径问题以及解决方法
    android 4高级编程(第三版)
  • 原文地址:https://www.cnblogs.com/qsl568/p/2420836.html
Copyright © 2020-2023  润新知