Android中的HTTP请求
生活随笔
收集整理的這篇文章主要介紹了
Android中的HTTP请求
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在Android 中發送HTTP請求的方式一般有兩種,HttpURLConnection和HttpClient,下面介紹的是HttpURLConnection的使用方法.
1.定義xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="button"></Button><ScrollViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"><TextViewandroid:text="sdsd"android:id="@+id/response_text"android:layout_width="wrap_content"android:layout_height="wrap_content" /></ScrollView> </LinearLayout>2.Activity文件
package com.emeet.one.elite.activity;import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.TextView;import com.emeet.one.elite.R; //import com.emeet.one.elite.service.HttpDownloader;import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL;public class DownLoaderDemo extends BaseActivity { // HttpDownloader downloader;public static final int SHOW_RESPONSE = 0;private Button sendRequest;private TextView responseText;private Handler handler = new Handler() {public void handleMessage(Message msg) {switch (msg.what) {case SHOW_RESPONSE:String response = (String) msg.obj;//在這里進行UI操作,將結果顯示到屏幕上responseText.setText(response);}}};class MyListener implements View.OnClickListener{@Overridepublic void onClick(View view) {//處理邏輯sendRequestWithHttpURLConnect();}}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_downloader);sendRequest = (Button) this.findViewById(R.id.button);responseText = (TextView) this.findViewById(R.id.response_text); // sendRequest.setOnClickListener(this);sendRequest.setOnClickListener(new MyListener());}public void onClick(View v) {if (v.getId() == R.id.button) {sendRequestWithHttpURLConnect();}}private void sendRequestWithHttpURLConnect() {//開啟線程來發起網絡請求new Thread(new Runnable() {@Overridepublic void run() {HttpURLConnection connection = null;try {URL url = new URL("https://blog.csdn.net/qq_42866164");connection = (HttpURLConnection)url.openConnection();connection.setRequestMethod("GET");connection.setConnectTimeout(8000);connection.setReadTimeout(8000);InputStream in = connection.getInputStream();//下面對獲取得到的輸入流進行讀取BufferedReader reader = new BufferedReader(new InputStreamReader(in));StringBuilder response = new StringBuilder();String line;while((line = reader.readLine()) != null) {response.append(line);}Message message = new Message();message.what = SHOW_RESPONSE;//將服務器返回的結果存放到Message中message.obj = response.toString();handler.sendMessage(message);}catch (Exception e ){e.printStackTrace();}finally {if (connection != null){connection.disconnect();}}}}).start();} }3. 在AndroidManifest中注冊Activity,并聲明網絡權限
<uses-permission android:name="android.permission.INTERNET" /><activityandroid:name=".activity.DownLoaderDemo"android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity>4. 效果展示
總結
以上是生活随笔為你收集整理的Android中的HTTP请求的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 葡萄浏览器平台是合法的吗(葡萄浏览器是不
- 下一篇: 我的世界如何快速学会速搭(我的世界速搭技