作为移动平台的应用,一定避免不了与网络交换数据,不论是读取网页数据,还是调用API接口,都必须掌握Http通信技术

使用Get方式与网络通信是最常见的Http通信,建立链接之后就可以通过输入流读取网络数据。
代码:

public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Button btn=(Button)findViewById(R.id.btn);        btn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {               //异步加载                new AsyncTask<String,Void,Void>(){                    @Override                    protected Void doInBackground(String... strings) {                        try {                            URL url=new URL(strings[0]);                            URLConnection connection=url.openConnection();//获取互联网连接                            InputStream is=connection.getInputStream();//获取输入流                            InputStreamReader isr=new InputStreamReader(is,"utf-8");//字节转字符,字符集是utf-8                            BufferedReader bufferedReader=new BufferedReader(isr);//通过BufferedReader可以读取一行字符串                            String line;                            while ((line=bufferedReader.readLine())!=null){                                Log.i("输出:",""+line);                            }                            bufferedReader.close();                            isr.close();                            is.close();                        } catch (MalformedURLException e) {                            e.printStackTrace();                        } catch (IOException e) {                            e.printStackTrace();                        }                        return null;                    }                    //使用api的数据接口                }.execute(" http://fanyi.youdao.com/openapi.do?keyfrom=testdemoHttpGet&key=660196743&type=data&doctype=xml&version=1.1&q=good ");            }        });    }}

布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" tools:context="custom.community.com.filedemo.MainActivity">    <Button  android:id="@+id/btn" android:text="读取数据" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>

AndroidManifest.xml配置网络

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="custom.community.com.filedemo">    <application  android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>  **<uses-permission android:name="android.permission.INTERNET"/>**</manifest>

更多相关文章

  1. 一句话锁定MySQL数据占用元凶
  2. android onSaveInstanceState方法
  3. 【Android】数据存储之Sharepreference
  4. Android(安卓)简单存储 SharedPreferencesUtils
  5. 【Android】listView动态处理item
  6. Android中Spinner下拉列表(使用ArrayAdapter和自定义Adapter实现)
  7. 最近项目里碰上了饼图,顺便整理了下几种Android饼图,以作参考
  8. Android数据流详解
  9. Android使用ImageLoader异步加载网络图片(二)结合listview

随机推荐

  1. Android(安卓)下的 bolts tasks
  2. Android之硬件加速
  3. Android(安卓)输入法/ 软键盘 显示模式
  4. Android中Activity启动模式详解
  5. Android之网络连接判断
  6. Android判断ListView滚动到最顶部第0条it
  7. Android日常整理(三)---intent打开浏览器、
  8. Android(安卓)Push
  9. android UI进阶之弹窗的使用(2)--实现通讯
  10. Android配置build.gradle自动化编译signi