1.概述与http协议介绍
2.检查网络连接状态
创建一个布尔类型的isNetworkConnected方法,如果context不为空,通过getSystemService得到一个ConnectivityManager的实例cm,再调用cm的getActiveNetworkInfo方法返回一个NetworkInfo的实例info,再判断如果info不为空,返回info的isAvailable方法。
然后在onClick方法判断isNetworkConnected的布尔值,若为true,toast网络可用,否则网络不可用。
最后在清单文件声明网络权限
判断wifi网络与mobile网络是否可用和上文大同小异,只不过是把info的getActiveInfo方法改为getNetworkInfo方法,并且传入ConnectivityManager的TYPE_WIFI或者TYPE_MOBILE标记
MainActivity
package com.example.networkstatetest; import android.app.Activity; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void checkNetworkClick(View view){ boolean result = isNetworkConnected(this); boolean result2 = isWifiNetworkConnected(this); if(result){ Toast.makeText(this, "网络可用", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(this, "网络不可用", Toast.LENGTH_SHORT).show(); } if(result2){ Toast.makeText(this, "wifi可用", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(this, "wifi不可用", Toast.LENGTH_SHORT).show(); } } public boolean isNetworkConnected(Context context){ if(context!=null){ ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = cm.getActiveNetworkInfo(); if(info!=null){ return info.isAvailable(); } } return false; } public boolean isWifiNetworkConnected(Context context){ if(context!=null){ ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo wifiInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if(wifiInfo!=null){ return wifiInfo.isAvailable(); } } return false; } }
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.networkstatetest.MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="检查网络状态" android:onClick="checkNetworkClick" /> </RelativeLayout>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
3.URL与显示网络图片
MainActivity
package com.example.shownetworkimagetest; import java.io.IOException; import java.io.InputStream; import java.lang.ref.WeakReference; import java.net.MalformedURLException; import java.net.URL; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.ImageView; public class MainActivity extends Activity { private static final int LOAD_SUCCESS = 0x1; private ImageView iv; private MyHandler handler = new MyHandler(this); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.imageView1); } private static class MyHandler extends Handler{ private final WeakReference<MainActivity> weakReference; public MyHandler(MainActivity activity){ weakReference = new WeakReference<MainActivity>(activity); } @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); MainActivity activity = weakReference.get(); if(activity!=null){ switch(msg.what){ case LOAD_SUCCESS: activity.iv.setImageBitmap((Bitmap) msg.obj); break; } } } } public void showClick(View view){ new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub String path = "http://cdn.duitang.com/uploads/item/201603/09/20160309133128_yz4JP.jpeg"; try { URL url = new URL(path); InputStream in = url.openStream(); Bitmap bitmap = BitmapFactory.decodeStream(in); Message message = handler.obtainMessage(LOAD_SUCCESS, bitmap); handler.sendMessage(message); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); } }
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.shownetworkimagetest.MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="显示网络图片" android:onClick="showClick" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignRight="@+id/button1" android:layout_below="@+id/button1" /> </RelativeLayout>
<uses-permission android:name="android.permission.INTERNET"/>
4.模拟登陆示例
5.ApacheHttpClient_简介与get请求
6.ApacheHttpClient_post请求