zoukankan      html  css  js  c++  java
  • 以JSONobject形式提交http请求

    private void initData() throws JSONException {
    OnLoadMyQuestionListener loadMyQuestionListener = questionConstantList -> {
    for (int i = 0; i < questionConstantList.size(); i++) {
    urlList.add("http://119.23.223.94" + questionConstantList.get(i).videoPath);
    }
    videoAdapter.notifyDataSetChanged();
    };

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("CP", "100");
    jsonObject.put("CI", new UserHelperTool(this).getHost().collegeID);
    jsonObject.put("NI", nodeConstant.NODE_UNIQUE_CODE);
    String URL = jsonObject.toString();
    LoadInformationStreamTask loadInformationStreamTask = new LoadInformationStreamTask();
    loadInformationStreamTask.setLoadMyQuestionListener1(loadMyQuestionListener);
    loadInformationStreamTask.execute(URL);
    }

    public static class LoadInformationStreamTask extends AsyncTask<String, Integer, List<QuestionConstant>> {
    private OnLoadMyQuestionListener loadMyQuestionListener1;

    void setLoadMyQuestionListener1(OnLoadMyQuestionListener loadMyQuestionListener1) {
    this.loadMyQuestionListener1 = loadMyQuestionListener1;
    }


    @Override
    protected void onPreExecute() {
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    protected List<QuestionConstant> doInBackground(String... params) {
    List<QuestionConstant> questionConstants = new ArrayList<>();
    HttpURLConnection connection;
    try {
    URL url = new URL(ServletValue.URLSQLQuestions);
    connection = (HttpURLConnection) url.openConnection();// 打开该URL连接
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");// 设置请求方法,“POST或GET”
    connection.setConnectTimeout(5000);// 设置连接建立的超时时间
    connection.setReadTimeout(50000);// 设置网络报文收发超时时间
    connection.setRequestProperty("Content-Type", "application/json");
    DataOutputStream os = new DataOutputStream(connection.getOutputStream());
    os.writeUTF(params[0]);
    os.flush();
    os.close();
    if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
    InputStreamReader in = new InputStreamReader(connection.getInputStream());
    JSONArray jsonArrayData = new JSONArray(new BufferedReader(in).readLine());//如果没有data会赋值null,用get的话会报错
    for (int i = 0; i < jsonArrayData.length(); i++) { //需要转成JSONObject再解析
    QuestionConstant questionConstant = new QuestionConstant();
    JSONObject objectToJson = jsonArrayData.getJSONObject(i);
    questionConstant.id = objectToJson.getInt("ID");
    questionConstant.collegeID = String.valueOf(objectToJson.getInt("CID"));
    questionConstant.question = objectToJson.getString("QT");
    questionConstant.createTime = objectToJson.getString("DT");
    questionConstant.videoPath = objectToJson.getString("VP");
    questionConstant.nodeID = objectToJson.getString("NID");
    questionConstant.userID = objectToJson.getString("SID");
    questionConstants.add(0, questionConstant);
    }

    }
    } catch (
    MalformedURLException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (
    JSONException e) {
    e.printStackTrace();
    }
    return questionConstants; // 这里返回的结果就作为onPostExecute方法的入参
    }

    @Override
    protected void onPostExecute(List<QuestionConstant> questionConstantList) {
    loadMyQuestionListener1.init(questionConstantList);
    }
    }
    --------------------- 

  • 相关阅读:
    在网页中实现截屏粘贴的功能
    CSS3 @font-face 做自定义图标
    Visual Studio报错一箩筐(持续更新)
    Axure实现vcg官网首页原型图
    Axure实现bootstrap首页线框图
    Web第一天——准备篇
    vue动态加载组件
    组件封装之将代码放到npm上
    node连接mysql生成接口,vue通过接口实现数据的增删改查(二)
    autoCAD2007 快捷键 标注
  • 原文地址:https://www.cnblogs.com/ly570/p/10970827.html
Copyright © 2011-2022 走看看