APKPure.apk(点击下载) / ATTAReporter.java


package com.tencent.raft.measure.report;

import android.net.TrafficStats;
import android.os.Build;
import com.tencent.raft.measure.RAFTMeasure;
import com.tencent.raft.measure.log.RLog;
import e.d.a.a.a;
import java.io.Closeable;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ATTAReporter {
    public static final String ATTA_TYPE = "Atta-Type";
    private static final String AT_URL = "https://h.trace.qq.com/kv";
    public static final String BATCH_REPORT = "batch-report";
    private static final String KEY_ATTA_ID = "attaid";
    public static final String KEY_CONTENT_LENGTH = "Content-Length";
    public static final String KEY_CONTENT_TYPE = "Content-Type";
    private static final String KEY_DATA = "datas";
    private static final String KEY_TOKEN = "token";
    private static final String KEY_TYPE = "type";
    private static final String KEY_VERSION = "version";
    private static final String METHOD_POST = "POST";
    private static final String TAG = "ATTAReporter";
    public static final int TIMEOUT = 300000;
    private static final String VAL_BATCH = "batch";
    public static final String VAL_TEXT_PLAIN_CHARSET_UTF_8 = "text/plain;charset=UTF-8";
    private static final String VAL_VERSION_DEFAULT = "v1.0.0";
    private final String attaId;
    private final String token;

    public ATTAReporter(String str, String str2) {
        this.attaId = str;
        this.token = str2;
    }

    private void closeQuietly(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e2) {
                e2.printStackTrace();
            }
        }
    }

    private void doPost(String str, String str2) {
        byte[] bArr;
        Closeable closeable;
        Throwable th;
        Exception e2;
        if (Build.VERSION.SDK_INT >= 26) {
            TrafficStats.getAndSetThreadStatsTag(26);
        }
        RLog.d(TAG, a.K("Req ", str, " msg ", str2));
        try {
            bArr = str2.getBytes("UTF-8");
        } catch (UnsupportedEncodingException unused) {
            bArr = new byte[0];
        }
        Closeable closeable2 = null;
        try {
            HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(str).openConnection();
            httpURLConnection.setRequestMethod(METHOD_POST);
            httpURLConnection.setConnectTimeout(TIMEOUT);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setRequestProperty(ATTA_TYPE, BATCH_REPORT);
            httpURLConnection.setRequestProperty("Content-Type", VAL_TEXT_PLAIN_CHARSET_UTF_8);
            httpURLConnection.setRequestProperty(KEY_CONTENT_LENGTH, String.valueOf(bArr.length));
            OutputStream outputStream = httpURLConnection.getOutputStream();
            try {
                outputStream.write(bArr);
                outputStream.flush();
                closeable = httpURLConnection.getInputStream();
                try {
                    if (RAFTMeasure.getAppConfig().isDebug()) {
                        RLog.d(TAG, "code = " + httpURLConnection.getResponseCode() + ", resp=" + httpURLConnection.getResponseMessage());
                    }
                    httpURLConnection.disconnect();
                    closeQuietly(outputStream);
                } catch (Exception e3) {
                    e2 = e3;
                    closeable2 = closeable;
                    closeable = closeable2;
                    closeable2 = outputStream;
                    try {
                        RLog.e(TAG, "doPost err", e2);
                        closeQuietly(closeable2);
                        closeQuietly(closeable);
                    } catch (Throwable th2) {
                        th = th2;
                        closeQuietly(closeable2);
                        closeQuietly(closeable);
                        throw th;
                    }
                } catch (Throwable th3) {
                    th = th3;
                    closeable2 = closeable;
                    closeable = closeable2;
                    closeable2 = outputStream;
                    closeQuietly(closeable2);
                    closeQuietly(closeable);
                    throw th;
                }
            } catch (Exception e4) {
                e2 = e4;
                closeable = closeable2;
                closeable2 = outputStream;
                RLog.e(TAG, "doPost err", e2);
                closeQuietly(closeable2);
                closeQuietly(closeable);
            } catch (Throwable th4) {
                th = th4;
                closeable = closeable2;
                closeable2 = outputStream;
                closeQuietly(closeable2);
                closeQuietly(closeable);
                throw th;
            }
        } catch (Exception e5) {
            e2 = e5;
            closeable = null;
            RLog.e(TAG, "doPost err", e2);
            closeQuietly(closeable2);
            closeQuietly(closeable);
        } catch (Throwable th5) {
            th = th5;
            closeable = null;
            closeQuietly(closeable2);
            closeQuietly(closeable);
            throw th;
        }
        closeQuietly(closeable);
    }

    public final void doPostBatchReport(JSONArray jSONArray) {
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put(KEY_ATTA_ID, this.attaId);
            jSONObject.put(KEY_TOKEN, this.token);
            jSONObject.put("type", VAL_BATCH);
            jSONObject.put("version", VAL_VERSION_DEFAULT);
            jSONObject.put(KEY_DATA, jSONArray);
            doPost(AT_URL, jSONObject.toString());
        } catch (JSONException e2) {
            RLog.e(TAG, "doPostBatchReport err", e2);
        }
    }
}