SoFunction
Updated on 2025-04-05

Two implementation methods for obtaining network data by HttpURLConnection and okHttp

Less nonsense, just upload the code. Simple and easy to understand.

xml is as follows:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:andro
 xmlns:tools="/tools"
 android:
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context="">
 <Button
  android:
  android:text="okhttp request"
  android:layout_width="150dp"
  android:layout_height="37dp"
  android:layout_alignBottom="@+id/http"
  android:layout_alignParentEnd="true"
  android:layout_alignParentTop="true" />

 <Button
  android:
  android:text="HTTP Request"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentTop="true"
  android:layout_alignParentStart="true" />

 <ScrollView
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_alignParentStart="true"
 android:layout_below="@+id/okhttp"
 android:layout_alignParentBottom="true"
 android:layout_alignParentEnd="true" >
  <TextView
   android:
   android:layout_width="match_parent"
   android:layout_height="match_parent" />
 </ScrollView>
</RelativeLayout>

The activity is as follows:

public class NetActivity extends AppCompatActivity implements {
private Button http,okhttp;
 private TextView stringData;
 private String web="";
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  (savedInstanceState);
  setContentView(.activity_net);
  http= (Button) findViewById();
  okhttp= (Button) findViewById();
  stringData= (TextView) findViewById();
  (this);
  (this);
 }

 @Override
 public void onClick(View v) {
  switch (()){
   case :
    sendRequestWithHttpURLConnection();
   break;
   case :
    sendRequestWithokHttp();
   break;
  }
 }
 private void sendRequestWithHttpURLConnection(){
  new Thread(new Runnable() {
   @Override
   public void run() {
    HttpURLConnection connection=null;
    BufferedReader reader=null;
    try {
     URL url=new URL(web);
     connection= (HttpURLConnection) ();
     ("GET");
     (5000);
     (5000);
     InputStream in=();
     reader=new BufferedReader(new InputStreamReader(in));
     StringBuffer response=new StringBuffer();
     String line;
     while ((line=())!=null){
      (line);
     }
     showRespond(());
    } catch (MalformedURLException e) {
     ();
    } catch (IOException e) {
     ();
    }finally {
     if (reader!=null){
      try {
       ();
      } catch (IOException e) {
       ();
      }
     }
    }
   }
  }).start();
 }

 private void showRespond(final String response) {
  runOnUiThread(new Runnable() {
   @Override
   public void run() {
    (response);
   }
  });
 }
 private void sendRequestWithokHttp(){
  new Thread(new Runnable() {
   @Override
   public void run() {

    try {
     OkHttpClient client=new OkHttpClient();
     Request request=new ()
       .url(web).build();
     Response response=(request).execute();
     String str=().string();
     showRespond(str);
    } catch (IOException e) {
     ();
    }
   }
  }).start();
 }
}


Note: Permission required

1.<uses-permission android:name=""/>

2.okhttp3 needs to add dependencies in gradle

dependencies {
 compile fileTree(dir: 'libs', include: ['*.jar'])
 androidTestCompile(':espresso-core:2.2.2', {
  exclude group: '', module: 'support-annotations'
 })
 compile ':appcompat-v7:24.2.1'
 compile ':design:24.2.1'
 compile '.okhttp3:okhttp:3.4.1'//rely testCompile 'junit:junit:4.12'
}

The above two ways to obtain network data are all the contents shared by the editor. I hope you can give you a reference and I hope you can support me more.