Android入门-使用Http协议访问网络

目标效果


1、操作基础知识



2、新建项目NetTest

3、设置activity_main.xml中的布局


代码为:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btnSendRequest"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="sendRequest"
        android:text="Send Request" />
<ScrollView 
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tvResponse"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />
</ScrollView>
</LinearLayout>
4、MainActivity.java代码

<span style="font-size:18px;">package com.example.nettest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
	private TextView tvResponse;
	public static final int SHOW_RESPONSE=1;//定义一个消息的类型
	private Handler handler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case SHOW_RESPONSE:
				String content=(String)msg.obj;
				tvResponse.setText(content);
				break;

			default:
				break;
			}
		};//处理消息
		 
	};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvResponse=(TextView) findViewById(R.id.tvResponse);
    }
    public void sendRequest(View view){
    	sendRequestWithHttpUrlConnection();
    }
	private void sendRequestWithHttpUrlConnection() {
		new Thread(){//将所有的操作都放于线程中
			public void run(){
				HttpsURLConnection httpsURLConnection=null;
				// TODO Auto-generated method stub
				try {
					URL url=new URL("http://www.baidu.com ");
				    httpsURLConnection=(HttpsURLConnection) url.openConnection();
					httpsURLConnection.setRequestMethod("GET");
					httpsURLConnection.setConnectTimeout(6000);
					InputStream is =httpsURLConnection.getInputStream();
					BufferedReader br=new BufferedReader(new InputStreamReader(is));
					String s;
					StringBuilder sb=new StringBuilder();
					while((s=br.readLine())!=null){
						sb.append(s);
					}
					Message message=new Message();
					message.what=SHOW_RESPONSE;
					message.obj=sb.toString();
					handler.sendMessage(message);//发出消息
					System.out.println("getContent="+sb.toString());
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally{
					httpsURLConnection.disconnect();//断开连接
					
				}
				
			};
			
		}.start();//启动线程
	}



}</span>
5、不要忘记AndroidManifest.xml中添加

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

</manifest>
程序完成



已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页