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);
    }
    }
    --------------------- 

  • 相关阅读:
    BZOJ 4199: [Noi2015]品酒大会 后缀自动机+逆序更新
    BZOJ 3676: [Apio2014]回文串 回文自动机
    Remember the Word UVALive
    [APIO2012]派遣 可并堆(左偏树)
    BZOJ 2555: SubString 后缀自动机 + LCT
    力扣题目汇总(转换成小写字母,唯一摩尔斯密码,有序数组平方)
    力扣题目汇总(加一,旋转数组,整数反转)
    力扣题目汇总(存在重复,合并两个有序数组,搜索插入位置)
    力扣题目汇总(买卖股票的最佳时机,最大连续1的个数,缺失的数字)
    力扣题目汇总(最长特殊序列,回文数,移动零)
  • 原文地址:https://www.cnblogs.com/ly570/p/10970827.html
Copyright © 2011-2022 走看看