0

HttpUrlConnection に基づいて OKHttp を使用する方が良いことはわかっていますが、その理由はわかりません。

実装された HTTPUrlConnection の代わりに OKHttp を使用する方が良いのはなぜですか?

そして.. 混合データを送信するにはどうすればよいですか。POST 変数とファイル。マルチスレッドが可能な場合に最適です。

これは、HttpUrlConnection を使用したクライアントの例です。OKHTTP ではどのように見えるでしょうか?

Dataclass と DataPackage は、送信するファイルとデータを保持するための単なるモーダルです。

Client if self は、以下を使用して FactoryManager (LazyLoading) で呼び出されます。

public static DefaultHttpPostClient getEnterpriseHttpManager() {
    if (defaultHttpPostClient == null) {
        defaultHttpPostClient = new DefaultHttpPostClient();
    }
    return defaultHttpPostClient;
}

私の HttpClient のコンストラクターは、呼び出されるたびに設定を要求します。

  public static HttpConnectionSettings getDefaultHttpConnectionSettings() throws MalformedURLException {
    HttpConnectionSettings httpConnectionSettings = new HttpConnectionSettings();
    httpConnectionSettings.setPost(true);
    httpConnectionSettings.setMultiThreaded(true);
    httpConnectionSettings.setUrl(new URL(MYURL);
    return httpConnectionSettings;
}

   public HttpResult executeRequest() {
    HttpURLConnection connection = null;
    OutputStream out = null;
    InputStream in = null;
    HttpResult httpResult = new HttpResult();
    try {
        connection = Mfactory.getOkHttpClient().open(httpConnectionSettings.getUrl());

        connection.setConnectTimeout(300000);
        connection.setUseCaches(false);
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestMethod((httpConnectionSettings.isPost()) ? "POST" : "GET");
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Cache-Control", "no-cache");
        connection.setReadTimeout(300000);
        connection.setDefaultUseCaches(true);
        connection.setRequestProperty("Accept-Charset", "UTF-8");
        if (httpConnectionSettings.isPost()) {
            connection.setRequestProperty("Content-Type", "multipart/form-data, boundary=XXXXXXXXXXYY");
        }

        out = connection.getOutputStream();

        MultipartEntity multipartEntity = new MultipartEntity();

        for (Object aMData : dataClass.getData().entrySet()) {
            Map.Entry mapEntry = (Map.Entry) aMData;
            String keyValue = (String) mapEntry.getKey();
            String value = (String) mapEntry.getValue();
            if (value != null) {
                multipartEntity.addPart(new StringPart(keyValue, value));
            } else {
                Log.d(this.getClass().getSimpleName(), "Value was null for key " + keyValue);
            }
        }


        for (DataPackage dataPackage : dataClass.getDataPackages()) {

            if (dataPackage.getbFile() != null) {
                multipartEntity.addPart(new BytePart("upfile", dataPackage.getbFile(), dataPackage.getfName()));
            } else if (!dataPackage.getsFile().equals("")) {
                multipartEntity.addPart(new FilePart(new File(dataPackage.getsFile()), dataPackage.getfName(), dataPackage.getMimetype()));
            } else if (dataPackage.getmFile() != null) {
                multipartEntity.addPart(new FilePart(dataPackage.getmFile(), dataPackage.getfName(), dataPackage.getMimetype()));
            }
            if (dataPackage.getMimetype() != null) {
                multipartEntity.addPart(new StringPart("mime", dataPackage.getMimetype()));
            }
            if (dataPackage.getFilesize() != 0) {
                multipartEntity.addPart(new StringPart("filesize_orig", String.valueOf(dataPackage.getFilesize())));
            }
            if (dataPackage.getFilepath() != null) {
                multipartEntity.addPart(new StringPart("filepath_orig", dataPackage.getFilepath()));
            }
            if (dataPackage.getfName() != null) {
                multipartEntity.addPart(new StringPart("filename_orig", dataPackage.getfName()));
            }
            if (dataPackage.getLastmodified() != 0) {
                multipartEntity.addPart(new StringPart("lastmodified_orig", String.valueOf(dataPackage.getLastmodified())));
            }
            if (dataPackage.getAdded() != 0) {
                multipartEntity.addPart(new StringPart("added_orig", String.valueOf(dataPackage.getAdded())));
            }
        }
        multipartEntity.writeTo(out);


        if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
            Log.e("httpResult", "Response: " + connection.getResponseCode() + " " + connection.getResponseMessage() + " content:" + connection.getURL());
            httpResult.setSuccess(false);
        } else {
            Log.d("httpResult", "Response: " + connection.getResponseCode() + " " + connection.getResponseMessage() + " content:" + connection.getURL());
            httpResult.setSuccess(true);
            httpResult.setInputStream(connection.getInputStream());
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            for (DataPackage dataPackage : dataClass.getDataPackages()) {
                if (dataPackage.isDeleteAfter()) {
                    new File(dataPackage.getFilepath()).delete();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (out != null) {
            try {
                out.close();
            } catch (Exception e) {
            }
        }
        if (in != null) {
            try {
                in.close();
            } catch (Exception e) {
            }
        }
        return httpResult;
    }
}

OKHttp に切り替えるのは理にかなっていますか? たくさんお願いがあります。約 100 のスレッドが開いている場合があります (Executor を持つ各スレッドは、独自の HttpRequest です)。

なぜ OKHttp を使用する必要があるのですか? これまでありがとう。

4

1 に答える 1