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>
程序完成



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值