国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

HttpURLConnection和okHttp兩種獲取網(wǎng)絡(luò)數(shù)據(jù)的實現(xiàn)方法

2019-10-22 18:15:58
字體:
供稿:網(wǎng)友

廢話少說,直接上代碼。簡單易懂。

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://www.baidu.com"; @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_net);  http= (Button) findViewById(R.id.http);  okhttp= (Button) findViewById(R.id.okhttp);  stringData= (TextView) findViewById(R.id.stringData);  http.setOnClickListener(this);  okhttp.setOnClickListener(this); } @Override public void onClick(View v) {  switch (v.getId()){   case R.id.http:    sendRequestWithHttpURLConnection();   break;   case R.id.okhttp:    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) url.openConnection();     connection.setRequestMethod("GET");     connection.setConnectTimeout(5000);     connection.setReadTimeout(5000);     InputStream in=connection.getInputStream();     reader=new BufferedReader(new InputStreamReader(in));     StringBuffer response=new StringBuffer();     String line;     while ((line=reader.readLine())!=null){      response.append(line);     }     showRespond(response.toString());    } catch (MalformedURLException e) {     e.printStackTrace();    } catch (IOException e) {     e.printStackTrace();    }finally {     if (reader!=null){      try {       reader.close();      } catch (IOException e) {       e.printStackTrace();      }     }    }   }  }).start(); } private void showRespond(final String response) {  runOnUiThread(new Runnable() {   @Override   public void run() {    stringData.setText(response);   }  }); } private void sendRequestWithokHttp(){  new Thread(new Runnable() {   @Override   public void run() {    try {     OkHttpClient client=new OkHttpClient();     Request request=new Request.Builder()       .url(web).build();     Response response=client.newCall(request).execute();     String str=response.body().string();     showRespond(str);    } catch (IOException e) {     e.printStackTrace();    }   }  }).start(); }}

注意:需要在加權(quán)限

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

2.okhttp3需要在gradle添加依賴

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

以上這篇HttpURLConnection和okHttp兩種獲取網(wǎng)絡(luò)數(shù)據(jù)的實現(xiàn)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 兴安县| 砀山县| 莲花县| 新蔡县| 多伦县| 临澧县| 攀枝花市| 北流市| 花莲市| 三台县| 桂林市| 夏邑县| 大荔县| 张家界市| 万盛区| 宁南县| 商水县| 陕西省| 濮阳市| 靖州| 顺昌县| 望江县| 威海市| 富宁县| 无为县| 襄城县| 年辖:市辖区| 西和县| 读书| 本溪| 无棣县| 土默特右旗| 常宁市| 长丰县| 岗巴县| 东辽县| 西乌| 额尔古纳市| 鄄城县| 葵青区| 德化县|