package com.yeungeek.okhttp;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String ENDPOINT = "https://www.wanandroid.com";
    private OkHttpClient mClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mClient = new OkHttpClient();
        findViewById(R.id.sync_btn).setOnClickListener(this);
        findViewById(R.id.async_btn).setOnClickListener(this);
    }

    private void syncMethod() {
        Request request = new Request.Builder()
                .url(ENDPOINT + "/banner/json/")
                .build();

        try {
            Response response = mClient.newCall(request).execute();
            Log.d("DEBUG", "##### response: " + response.body().string());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void asyncMethod() {
        Request request = new Request.Builder()
                .get()
                .url(ENDPOINT + "/wxarticle/chapters/json")
                .build();

        mClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("DEBUG", "##### onFailure: ", e);

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.d("DEBUG", "##### response: " + response.body().string());

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sync_btn:
                Log.d("DEBUG", "##### click sync");
                new Thread(() -> {
                    syncMethod();
                }).start();
                break;
            case R.id.async_btn:
                asyncMethod();
                break;
        }
    }
}

@@ -166,27 +166,230 @@
 java.io.
-IOException
+ByteArrayOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.net.HttpURLConnection;%0Aimport java.net.MalformedURLException;%0Aimport java.net.URL;%0Aimport java.nio.charset.StandardCharsets
 ;%0A%0Aimpor
@@ -1029,32 +1029,1113 @@
 r(this);%0A    %7D%0A%0A
+    private void urlConnection() %7B%0A        try %7B%0A            URL url = new URL(ENDPOINT + %22/banner/json/%22);%0A            HttpURLConnection connection = (HttpURLConnection) url.openConnection();%0A            connection.setRequestMethod(%22GET%22);%0A            connection.connect();%0A%0A            int code = connection.getResponseCode();%0A            Log.d(%22DEBUG%22, %22##### response code: %22 + code);%0A            if (code == HttpURLConnection.HTTP_OK) %7B%0A                InputStream is = connection.getInputStream();%0A%0A                ByteArrayOutputStream bos = new ByteArrayOutputStream();%0A                byte%5B%5D buffer = new byte%5B2048%5D;%0A                int length;%0A                while ((length = is.read(buffer)) != -1) %7B%0A                    bos.write(buffer, 0, length);%0A                %7D%0A%0A                String result = bos.toString(StandardCharsets.UTF_8.name());%0A                bos.close();%0A                is.close();%0A%0A                Log.d(%22DEBUG%22, %22##### response result: %22 + result);%0A            %7D%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A        %7D%0A    %7D%0A%0A
     private void
@@ -3337,16 +3337,18 @@
 () -%3E %7B%0A
+//
         
@@ -3365,32 +3365,69 @@
   syncMethod();%0A
+                    urlConnection();%0A
                 

update all files