以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> {
    private OnLoadMyQuestionListener loadMyQuestionListener1;

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


    @Override
    protected void onPreExecute() {
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    protected List doInBackground(String... params) {
        List 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 questionConstantList) {
        loadMyQuestionListener1.init(questionConstantList);
    }
}

你可能感兴趣的:(安卓)