【高分悬赏】解析多层嵌套的json数据,取出json数据中的MP4文件

通过okhttp网络请求方式获取到接口里参数path的MP4文件,麻烦写下完整的代码。我不知道怎么解析返回的数据

我的代码如下所示:
public class MainActivity extends AppCompatActivity {
final OkHttpClient client = new OkHttpClient();
// private final Gson gson = new Gson();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getRequest();
}

private void getRequest() {

    final Request request = new Request.Builder()
            .get()
            .tag(this)
            .url("http://huaruanyun.cn:19001/get_play_list?devno=7C0CC5C2E3D69960")
            .build();

    new Thread(new Runnable() {
        @Override
        public void run() {
            Response response = null;
            try {
                response = client.newCall(request).execute();
                if (response.isSuccessful()) {

                    String result = response.body().string();

                    Log.i("qp", "打印GET响应的数据:" + result);
                } else {
                    throw new IOException("Unexpected code " + response);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

}

String result = response.body().string();
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonItems = jsonObject.getJSONArray("items");
for (int i = 0; i < jsonItems.length(); i++) {

JSONArray jsonData = jsonItems.getJSONObject(i).getJSONArray("tpl_data");
for (int e = 0; e < jsonData.length(); e++) {
    JSONArray jsonContent = jsonData.getJSONObject(e).getJSONArray("content");

    for (int n = 0; n < jsonContent.length(); n++) {
        path = jsonContent.getJSONObject(0).getString("path");


    }
}

}

首先是http工具类,如下

public class HttpClientUtils {
    private static final int TIME_OUT = 1000 * 6; // 超时
    private static final String METHOD_POST = "POST";
    private static final String METHOD_GET = "GET";
    private static final int HTTP_OK = 200;
    private static final String CHARTSET = "UTF-8"; // 字符编码
    private static final int BUFFER = 1024 * 8;// 缓冲区

    public static String get(String urlStr) throws Exception {
        //_FakeX509TrustManager.allowAllSSL();
        L.d("get:" + urlStr);
        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        InputStream inStream = null;
        String response = null;
        try {
            url = new URL(urlStr);
            // 根据URL打开远程连接
            conn = (HttpURLConnection) url.openConnection();
            // 设置参数
            conn.setDoInput(true);
            conn.setConnectTimeout(TIME_OUT);
            conn.setRequestMethod(METHOD_GET);
            conn.setRequestProperty("accept", "*/*");
            // 建立连接
            conn.connect();
            // 接受返回码
            int responseCode = conn.getResponseCode();
            if (responseCode == HTTP_OK) {
                // 获取输入流
                inStream = conn.getInputStream();
                // 从输入流中获取信息
                response = getResponse(inStream);
                L.d("HttpClientUtils get 返回成功:" + response);
            } else {
                // 请求失败
                response = "返回码:" + responseCode;
                L.d("HttpClientUtils get 返回失败:" + response);
            }
        } catch (Exception e) {
            throw e;
        } finally {
            // 关闭连接
            conn.disconnect();
        }
        return response;
    }

    public static String post(String urlStr, String paramsStr) throws Exception {
        L.d("post:" + urlStr + "?" + paramsStr);
        // 把参数转换成字符数组
        byte[] data = paramsStr.getBytes();
        URL url = null;
        HttpURLConnection conn = null;
        InputStream inStream = null;
        String response = null;
        try {
            url = new URL(urlStr);
            // 根据URL打开远程连接
            conn = (HttpURLConnection) url.openConnection();
            // 设置参数
            conn.setConnectTimeout(TIME_OUT);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod(METHOD_POST);
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Charset", CHARTSET);
            conn.setRequestProperty("Content-Length", String.valueOf(data.length));
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            // 建立连接
            conn.connect();
            DataOutputStream outputStream = new DataOutputStream(conn.getOutputStream());
            // 把字节数组写到六种
            outputStream.write(data);
            // 发送数据
            outputStream.flush();
            outputStream.close();
            // 获取返回码
            int responseCode = conn.getResponseCode();
            if (responseCode == HTTP_OK) {
                // 获取输入流
                inStream = conn.getInputStream();
                // 从输入流中获取信息
                response = getResponse(inStream);
                L.d("HttpClientUtils post 返回成功:" + response);
            } else {
                // 请求失败
                response = "返回码:" + responseCode;
                L.d("HttpClientUtils post 返回失败:" + response);
            }
        } catch (Exception e) {
            throw e;
        } finally {
            // 关闭连接
            conn.disconnect();
        }
        return response;
    }

    public static String postWithFile(String httpUrl, String queryString, List<Parameter> files) throws Exception {
//      _FakeX509TrustManager.allowAllSSL();
        L.d("postWithFile:" + httpUrl + "?" + queryString);
        // 数据分隔线
        final String BOUNDARY = "---------------------------7da2137580612";
        // 回车
        final String RETURN = "\r\n";
        // 前缀
        final String PREFIX = "--";

        HttpURLConnection conn = null;
        InputStream inStream = null;
        String response = null;
        try {
            URL url = new URL(httpUrl);
            // 根据URL打开远程连接
            conn = (HttpURLConnection) url.openConnection();
            // 设置参数
            conn.setRequestMethod(METHOD_POST);
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Charset", CHARTSET);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + BOUNDARY);

            DataOutputStream out = new DataOutputStream(conn.getOutputStream());
            // 传送文本类型参数
            if (queryString != null && !queryString.equals("")) {
                // 分割文本类型参数字符串
                String[] params = queryString.split("&");
                for (String str : params) {
                    if (str != null && !str.equals("")) {
                        if (str.indexOf("=") > -1) {
                            String[] param = str.split("=");
                            // 获取参数值
                            String value = (param.length == 2 ? decode(param[1]) : "");
                            // 添加数据分隔线
                            out.writeBytes(PREFIX + BOUNDARY + RETURN);
                            // 添加参数名
                            out.writeBytes("Content-Disposition: form-data; name=\"" + param[0] + "\"" + RETURN);
                            // 添加\r\n
                            out.writeBytes(RETURN);
                            // 添加参数的值
                            out.write(value.getBytes(CHARTSET));
                            // 添加\r\n
                            out.writeBytes(RETURN);
                        }
                    }
                }
            }
            // 遍历文件列表
            for (Parameter file : files) {
                // 获取文件名
                String fileName = file.getValue().substring(file.getValue().lastIndexOf("/") + 1);
                // 添加数据分隔线
                out.writeBytes(PREFIX + BOUNDARY + RETURN);
                // 添加文件信息
                out.writeBytes("Content-Disposition: form-data; name=\"" + file.getName() + "\"; filename=\"" + fileName + "\"" + RETURN);
                // 添加\r\n
                out.writeBytes(RETURN);

                // 创建文件输入流
                FileInputStream fis = new FileInputStream(file.getValue());
                // 设置缓冲区
                byte[] buffer = new byte[BUFFER];
                int count = 0;
                while ((count = fis.read(buffer)) != -1) {
                    out.write(buffer, 0, count);
                }
                fis.close();
                out.writeBytes(RETURN);
            }
            // 添加数据分隔线
            out.writeBytes(PREFIX + BOUNDARY + PREFIX + RETURN);
            out.flush();
            out.close();

            // 获取返回码
            int responseCode = conn.getResponseCode();
            if (responseCode == HTTP_OK) {
                // 获取输入流
                inStream = conn.getInputStream();
                // 从输入流中获取信息
                L.d("HttpClientUtils post pic 返回成功:" + response);
            } else {
                // 请求失败
                response = "返回码:" + responseCode;
                L.d("HttpClientUtils post pic 返回失败:" + response);
            }

        } catch (Exception e) {
            throw e;
        } finally {
            // 关闭连接
            conn.disconnect();
        }
        return response;
    }

    private static String getResponse(InputStream inStream) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        int len = -1;
        byte[] buffer = new byte[BUFFER];// 缓冲区
        while ((len = inStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }
        byte[] data = outputStream.toByteArray();
        return new String(data);
    }

    public static String decode(String s) {
        if (s == null) {
            return "";
        }
        try {
            return URLDecoder.decode(s, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}

然后就是activity调用

public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String result = null;
        try {
            result = HttpClientUtils.get("huaruanyun.cn:19001/get_play_list?devno=7C0CC5C2E3D69960");
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jo=new JSONObject(result);
        String path=jo.getString("path");
        System.out.println(path);
    }
}

http请求可根据自身需求,使用get或者post,传对应的参数即可