Android 学习之Network

Tikitoo

2014/12/30

介绍

平常我们使用浏览器来浏览网页,但是作为开发者,就需要做来实现这些功能;那怎么才能实现呢?那就要学习网络编程,底层要牵涉到TCP/IP 协议,HTTP 协议,只有了解了这些才能做网络开发;

网络编程的几种方式

java.net – (Socket, URL)

org.apache -(HttpRequest, HttpResponse)

android.net – (URI, AndroidHttpClient, AudioStream)

Third Libearys(okHttp, Vollet etc)

查看是否连接网络

ConnectivityManager check = (ConnectivityManager) 
this.context.getSystemService(Context.CONNECTIVITY_SERVICE);  

NetworkInfo[] info = check.getAllNetworkInfo();


for (int i = 0; i<info.length; i++){
   if (info[i].getState() == NetworkInfo.State.CONNECTED){
      Toast.makeText(context, "Internet is connected
      Toast.LENGTH_SHORT).show();
   }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="48dp">

    <ImageView
        android:id="@+id/ivIcon"
        android:layout_width="25dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:layout_centerVertical="true" />

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/ivIcon"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:gravity="center_vertical"
        android:paddingRight="40dp"/>

</RelativeLayout>

activity_main.xml

Made with Slides.com