李堇 2023-10-10 15:07 采纳率: 52.8%
浏览 8

使用安卓模拟器出现unexpected end of stream

使用as的模拟器时,页面调用接口经常会出现Exception:unexpected end of stream on com.android.okhttp.Address@b32e4744这个错误,不知道是哪里出现了问题?请问哪位遇到并解决过此类问题?
代码如下:


//页面初始化的时候调用该方法
protected void initDatas() {
      //LogUtils.e"=1=","initDatas:");
        if (NetUtils.isConnected(this.getContext())) {
            new getSchoolClassNotiticationMessagesListxyt().execute("");
        } else {
            //NoNetAdapter();
            ToastUtil.showShort(this.getContext(), R.string.net_notConnectedHint);
        }
    }


 /**
     * 获取信息
     */
    class getSchoolClassNotiticationMessagesListxyt extends AsyncTask<String, Void, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            //if (!isFinishing()) {
            mProDialog.setMessage(getResources().getString(R.string.load_msg));
            mProDialog.show();
            //}
        }

        @Override
        protected String doInBackground(String... params) {
            String url = HttpConstant.getnotiticationMessagesListIntelligence;
            TRNotiticationMessages nm = new TRNotiticationMessages();
            nm.setMchnId(mchnId);
          
            nm.setPageNo(nowNum);
            nm.setPageSize(pageSize);
            nm.setNear(near2);
            nm.setGrade(grade);
            String[] printModules = {Constants.PRINTER_MODULE_0034,Constants.PRINTER_MODULE_0035};
            nm.setPrinterModules(printModules);
            String postString = JsonUtils.javaBeanToJson(nm);
            //LogUtils.e("=1=","postString:" + postString);
            String json = HttpConnectionUtil.AccessUrl(getContext(),url,postString);
            //LogUtils.e("=1=","json:" + json);
            return json;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            if (mProDialog != null && mProDialog.isShowing()) {
                mProDialog.cancel();
            }
            //LogUtils.e("=1=","result:" + result);
            if (result != null && !result.equals("")) {
                @SuppressWarnings("unchecked")
                ApiResponse<String> info = JsonUtils.jsonToJavaBean(result, ApiResponse.class);
              //LogUtils.e"=2=","info:" + info);
                if (info == null) {
                    ToastUtil.showShort(getContext(), R.string.getData_fail);
                    return;
                }

                if (info.getCode() != null && info.getCode().equals("10000") && info.getData() != null) {
                  
                    String jsonArray = JSONArray.toJSONString(info.getData());
                 
                    list = JsonUtils.jsonToJavaBeanList(jsonArray, TRNotiticationMessages.class);

                    alllist.addAll(list);
                  
                    listView.setAdapter(mAdapter);

                    
                }

            } else {
              
                ToastUtil.showShort(getContext(), R.string.net_notConnectedHint);
            }
        }
    }


public static String AccessUrl(Context context,String url, String postString) {
        String result = null;
        HttpURLConnection conn = null;
        try {
//            Log.d("HttpUtil","-----url------"+url);
            URL postUrl = new URL(url);
            conn = (HttpURLConnection) postUrl.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestProperty("content-type","application/json;charset=UTF-8");
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(15000);
            String sessionid = SharedPreferencesUtil.getString(context,"sessionid","");
            if(sessionid != null) {
                conn.setRequestProperty("cookie", sessionid);
            }
            OutputStream os = conn.getOutputStream();
            os.write(postString.getBytes("UTF-8")); // 往远程URL传递参数
            os.flush();
            os.close();
            int code = conn.getResponseCode();
            //Log.d("HttpUtil","-----code------"+code);
            conn.getHeaderFields();
            conn.getContentLength();
            if (code == 200) {// 返回成功
                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
                String line;
                StringBuffer buffer = new StringBuffer();
                while ((line = reader.readLine()) != null) {
                    buffer.append(line);
                }
                result = buffer.toString();
//                LogUtils.d("==","返回result:" + result);
                //Token过期,登录,需重新请求
                if (result != null && result.equals("INVALID_TOKEN")){
                    //登录
                    String studentAccent = SharedPreferencesUtil.getString(context, "studentAccent", null);
                    String passwd = SharedPreferencesUtil.getString(context, "passwd", null);
                    String mchnId = MchnUtils.getMchnId();
                    if (studentAccent != null && !studentAccent.equals("")){
                        TBStudent student = new TBStudent();
                        student.setStudentAccent(studentAccent);
                        student.setPassword(passwd);
                        student.setMchnId(mchnId);
                        String url2 = HttpConstant.login;
                        String postString2 = JsonUtils.javaBeanToJson(student);
                        String result2 = HttpConnectionUtil.AccessUrl(url2,postString2);

//                        LogUtils.e("=1=","json:" + result2);
                        if (result2 != null && !result2.equals("")) {
                            @SuppressWarnings("unchecked")
                            ApiResponse<TBStudent> info = JsonUtils.jsonToJavaBean(result2, ApiResponse.class);
//                            LogUtils.e("=2=","info:" + info);
                            if (info == null) {
                                return null;
                            }
                            if (info.getCode() != null && info.getCode().equals("10000")) {
                                LoginUser loginUser = (LoginUser)JsonUtils.jsonToJavaBean(JSONArray.toJSONString(info.getData()), LoginUser.class);
//                                LogUtils.e("=4=","loginUser:" + loginUser.toString());

                                //保存登录信息
                                SharedPreferencesUtil.putString(context,"studentAccent", studentAccent);
                                SharedPreferencesUtil.putString(context,"passwd", passwd);
                                SharedPreferencesUtil.putString(context,"studentId",loginUser.getStudentId());
                                SharedPreferencesUtil.putString(context,"studentName",loginUser.getStudentName());
                                SharedPreferencesUtil.putString(context,"grade",loginUser.getGrade());
                                SharedPreferencesUtil.putString(context,"sessionId",loginUser.getSessionId());
                                SharedPreferencesUtil.putString(context,"sessionExpires",loginUser.getSessionExpires());
                                SharedPreferencesUtil.putString(context,"regFlag",loginUser.getRegFlag());
                                SharedPreferencesUtil.putString(context,"token",loginUser.getToken());
                                SharedPreferencesUtil.putString(context,"uuid",loginUser.getUuid());
                                SharedPreferencesUtil.putString(context,"stringDate",loginUser.getStringDate());
                                SharedPreferencesUtil.putString(context,"isVip",loginUser.getIsVip());
                                SharedPreferencesUtil.putString(context,"telephone",loginUser.getTelephone());
                            }
                            //重新请求业务
                            return  AccessUrl(context,url,postString);
                        }
                    }
                }
            } else {
                LogUtils.e("==","返回失败:" + code);
            }
        } catch (MalformedURLException e) {
            LogUtils.e("==","Exception:" + e.getMessage());
        } catch (Exception e) {
            LogUtils.e("==","Exception:" + e.getMessage());
        }finally{
            if(conn != null){
                conn.disconnect();
                conn=null;
            }
        }
        return result;
    }

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-10-10 18:17
    关注

    【相关推荐】




    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

问题事件

  • 创建了问题 10月10日

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line