「Android/HttpRequest通信/okhttp3」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: エラーが起こる場合) |
|||
(同じ利用者による、間の7版が非表示) | |||
行10: | 行10: | ||
implementation 'com.squareup.okhttp3:okhttp:3.7.0' | implementation 'com.squareup.okhttp3:okhttp:3.7.0' | ||
} | } | ||
− | |||
参考:https://qiita.com/naoi/items/8d493f00b0bbbf8a666c | 参考:https://qiita.com/naoi/items/8d493f00b0bbbf8a666c | ||
行20: | 行19: | ||
import okhttp3.Response; | import okhttp3.Response; | ||
import okhttp3.ResponseBody; | import okhttp3.ResponseBody; | ||
+ | String url = "https://api.github.com/feeds"; | ||
OkHttpClient client = new OkHttpClient(); | OkHttpClient client = new OkHttpClient(); | ||
try { | try { | ||
行34: | 行34: | ||
==非同期処理== | ==非同期処理== | ||
+ | import android.os.Handler; | ||
+ | import android.os.Looper; | ||
+ | import android.util.Log; | ||
+ | import java.io.IOException; | ||
import okhttp3.Call; | import okhttp3.Call; | ||
+ | import okhttp3.Callback; | ||
import okhttp3.OkHttpClient; | import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | import okhttp3.Request; | ||
import okhttp3.Response; | import okhttp3.Response; | ||
import okhttp3.ResponseBody; | import okhttp3.ResponseBody; | ||
+ | String url = "https://api.github.com/feeds"; | ||
OkHttpClient client = new OkHttpClient(); | OkHttpClient client = new OkHttpClient(); | ||
// RequestBody requestbodey = new FormBody.Builder() | // RequestBody requestbodey = new FormBody.Builder() | ||
行64: | 行70: | ||
参考:http://nandarou.hateblo.jp/entry/2017/05/23/180728 | 参考:http://nandarou.hateblo.jp/entry/2017/05/23/180728 | ||
+ | |||
+ | ==javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: エラーが起こる場合== | ||
+ | Android 4.4でエラーが起こるらしい(Android4.1.1で自分はこのエラーを確認した) | ||
+ | |||
+ | 以下コードを入れたが、結局エラーが起きたままだった・・・。また起きたら対応を考える・・・・。 | ||
+ | |||
+ | List<CipherSuite> cipherSuites = new ArrayList<>(); | ||
+ | cipherSuites.addAll(ConnectionSpec.MODERN_TLS.cipherSuites()); | ||
+ | cipherSuites.add(CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA); | ||
+ | cipherSuites.add(CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA); | ||
+ | ConnectionSpec legacyTls = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) | ||
+ | .cipherSuites(cipherSuites.toArray(new CipherSuite[0])) | ||
+ | .build(); | ||
+ | OkHttpClient client = new OkHttpClient.Builder() | ||
+ | .connectionSpecs(Arrays.asList(legacyTls, ConnectionSpec.CLEARTEXT)) | ||
+ | .build(); | ||
+ | |||
+ | 参考:https://github.com/square/okhttp/issues/4053 |
2018年9月5日 (水) 18:12時点における最新版
目次
必須
Android 2.3以降
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
インストール
app/build.gradle
dependencies { implementation 'com.squareup.okhttp3:okhttp:3.7.0' }
参考:https://qiita.com/naoi/items/8d493f00b0bbbf8a666c
同期処理
import okhttp3.Call; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import okhttp3.ResponseBody; String url = "https://api.github.com/feeds"; OkHttpClient client = new OkHttpClient(); try { Request request = new Request.Builder().url(url).build(); Call call = client.newCall(request); Response response = call.execute(); ResponseBody body = response.body(); String res = body.string(); Log.i("okhttp3", "res=" + res); } catch (IOException e) { }
同期処理なので、NetworkOnMainThreadExceptionエラーが出る場合はAcyncTask側で処理する必要がある
非同期処理
import android.os.Handler; import android.os.Looper; import android.util.Log; import java.io.IOException; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import okhttp3.ResponseBody; String url = "https://api.github.com/feeds"; OkHttpClient client = new OkHttpClient(); // RequestBody requestbodey = new FormBody.Builder() // .build(); Request request = new Request.Builder() .url(url) //.post(requestbodey) .get() .build(); client.newCall(request).enqueue(new Callback() { final Handler mainHandler = new Handler(Looper.getMainLooper()); @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); } @Override public void onResponse(Call call, Response response) throws IOException { String result = response.body().string(); String callString = call.toString(); Log.i("okhttp3", "res=" + result); } });
非同期なのでメインスレッド側に記述できる
参考:http://nandarou.hateblo.jp/entry/2017/05/23/180728
javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: エラーが起こる場合
Android 4.4でエラーが起こるらしい(Android4.1.1で自分はこのエラーを確認した)
以下コードを入れたが、結局エラーが起きたままだった・・・。また起きたら対応を考える・・・・。
List<CipherSuite> cipherSuites = new ArrayList<>(); cipherSuites.addAll(ConnectionSpec.MODERN_TLS.cipherSuites()); cipherSuites.add(CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA); cipherSuites.add(CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA); ConnectionSpec legacyTls = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) .cipherSuites(cipherSuites.toArray(new CipherSuite[0])) .build(); OkHttpClient client = new OkHttpClient.Builder() .connectionSpecs(Arrays.asList(legacyTls, ConnectionSpec.CLEARTEXT)) .build();