お世話になってます。
Android開発初心者です。
現在古いアプリを最新のアプリに差し替えを行っております。
その上で問題となっています。
org.apache.httpの変更です。
調べてみると
build.gradleに
useLibrary 'org.apache.http.legacy'
を足すと使用することはできる
もしくはHttpURLConnectionを使ってくださいとありました。
いつか使用でき無くなることを考えると修正を行いたいので
表題にもあるように使用するAPIをHttpURLConnectionに変更を希望しております。
行き詰っているのは
Activityクラスの中に
implements ResponseHandler<Void>
と宣言されており
handleResponseが実装されております。
これをHttpURLConnectionで表現しようとするとどういった形になりますでしょうか?
それともこれ以外のレスポンス系のAPIが必要になるのでしょうか
ご教示お願いいたします。
ActivationActivity
1 public final Void handleResponse(final HttpResponse response) 2 throws IOException { 3 if ((response == null) || (!Util.isSuccess(response))) { 4 // 通信失敗 5 postMsgBox(getResStr(R.string.errtitle), 6 getResStr(R.string.connection_err)); 7 return null; 8 } 9 try { 10 SettingRes res = new SettingRes(response); 11 int status = res.getStatus(); 12 13 switch (status) { 14 case Res.OK: 15中略 16 17 protected final void onCreate(final Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_activation); 20 adjustWindowsSize(); 21 22 btnOK = (Button) findViewById(R.id.btnOK); 23 btnCancel = (Button) findViewById(R.id.btnCancel); 24 etAct = (EditText) findViewById(R.id.etAct); 25 etActPass = (EditText) findViewById(R.id.etActPass); 26 27 btnOK.setOnClickListener(new OnClickListener() { 28 29 @Override 30 public void onClick(final View v) { 31中略 32 if (Util.getSetting(ActivationActivity.this, 33 TR_SETTING.URL) != null) { 34 actreq.setUrl((String) Util.getSetting( 35 ActivationActivity.this, TR_SETTING.URL)); 36 } 37 38 sendRequestUI(actreq, ActivationActivity.this); 39 } 40 }); 41 42 43
BaseActivity
1各Activityから"sendRequestUI"を受け取って共通処理を行い"sendRequest"を呼び出しております。 2Async処理を行っております。
sendRequest
1 public static void sendRequest(final Req req, 2 final Handler handler) { 3 4 /** 送信用スレッドを定義するためのクラス */ 5 class SendThread implements Runnable { 6 7 private final Req mParamReq; 8 private final Handler mHandler; 9 10 /** 11 * コンストラクタ 12 * 13 * @param req 14 * リクエスト 15 * @param handler 16 * レスポンスを処理するハンドラ 17 */ 18 public SendThread(final Req req, 19 final Handler handler) { 20 mParamReq = req; 21 mHandler = handler; 22 } 23 24 @Override 25 public void run() { 26*****************************新規部分***************************** 27 try { 28 final Request request = new Request.Builder() 29 .url(mParamReq.toQueryString()) 30 .get() 31 .build(); 32 OkHttpClient client = new OkHttpClient(); 33 DebugLog.out(mParamReq.toQueryString()); 34 35 client.newCall(request).enqueue(new Callback() { 36 public void onResponse(Call call, Response response) throws IOException { 37 38 } 39 public void onFailure(Call call, final IOException e) { 40 41 } 42 43 }); 44 45*****************************新規部分***************************** 46*****************************既存部分*****************************(削除予定) 47 final int iNTWKTIMEOUT = 3000; 48 try { 49 DefaultHttpClient client = new DefaultHttpClient(); 50 HttpParams params = client.getParams(); 51 HttpConnectionParams.setConnectionTimeout(params, 52 iNTWKTIMEOUT); // 接続のタイムアウトを3秒にする 53 54 DebugLog.out(mParamReq.toQueryString()); 55 HttpGet get = new HttpGet( 56 mParamReq.toQueryString()); 57 client.execute(get, mHandler); 58 } catch (ClientProtocolException e) { 59 return; 60*****************************既存部分*****************************(削除予定) 61 } catch (IOException e) { 62 // ネットワークが繋がらない場合 63 // e.printStackTrace(); 64 try { 65 // ネットワークが繋がらない場合、nullのレスポンスを返す 66 mHandler.handleResponse(null); 67 } catch (Exception e1) { 68 return; 69 } 70 } 71 } 72 } 73 74 SendThread mRunnableSender = new SendThread(req, handler); 75 Thread th = new Thread(mRunnableSender, "ReceiveTR"); 76 77 th.run(); 78 } 79 80
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/20 03:29
2018/09/20 07:53
2018/09/20 10:17
2018/09/20 23:55
2018/09/21 03:40 編集
2018/09/21 04:35
2018/09/21 04:56 編集
2018/09/21 05:04
2018/09/21 05:18
2018/09/21 05:28