废话少说,直接上代码。简单易懂。
xml如下:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_net" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.waterlamp.NetActivity"> <Button android:id="@+id/okhttp" android:text="okhttp请求" android:layout_width="150dp" android:layout_height="37dp" android:layout_alignBottom="@+id/http" android:layout_alignParentEnd="true" android:layout_alignParentTop="true" /> <Button android:id="@+id/http" android:text="HTTP请求" 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:id="@+id/stringData" android:layout_width="match_parent" android:layout_height="match_parent" /> </ScrollView></RelativeLayout>activity如下:
public class NetActivity extends AppCompatActivity implements View.OnClickListener{private Button http,okhttp; private TextView stringData; private String web="https://pile 'junit:junit:4.12'}以上这篇HttpURLConnection和okHttp两种获取网络数据的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。