Android Studio学习路程

世事洞明皆学问,人情练达即文章。这篇文章主要讲述Android Studio学习路程相关的知识,希望能为你提供帮助。
【Android Studio学习路程】今天学习怎样调用互联网的API,下面是一些代码:

1 package com.example.hp.app3; 2 3 /* 4 学会调用互联网API(公共的) 5*/ 6 7 import android.os.Bundle; 8 import android.os.Handler; 9 import android.os.Message; 10 import android.support.v7.app.ActionBarActivity; 11 import android.util.Log; 12 import android.view.View; 13 import android.widget.TextView; 14 15 import java.io.BufferedReader; 16 import java.io.InputStream; 17 import java.io.InputStreamReader; 18 import java.net.HttpURLConnection; 19 import java.net.URL; 20 21 public class MainActivity extends ActionBarActivity implements View.OnClickListener { 22privateint GET_DATA_SUCCESS = 101; //获取信息成功的标志 23private TextView mTvShow; 24private BufferedReader bufferedreader; 25private InputStream inputstream; 26private StringBuilder stringBuilder; 27private HttpURLConnection connection; 28 29//多线程传输数据Handler 30Handler mHandler = new Handler(new Handler.Callback() { 31@Override 32public boolean handleMessage(Message message) { 33if(message.what==GET_DATA_SUCCESS){ 34String data = https://www.songbingjia.com/android/message.getData().getString("data"); 35Log.i("MainActivity",data); 36mTvShow.setText(data); 37} 38return false; 39} 40}); 41 42@Override 43protected void onCreate(Bundle savedInstanceState) { 44super.onCreate(savedInstanceState); 45setContentView(R.layout.activity_main); 46 47//初始化控件 48initUI(); 49//初始化数据 50initData(); 51} 52 53private void initUI() { 54//获取文本框 55mTvShow = (TextView) findViewById(R.id.tv_show); 56//获取按钮 57findViewById(R.id.btn).setOnClickListener(this); 58} 59 60@Override 61public void onClick(View view) { 62//初始化数据 63initData(); 64} 65 66private void initData() { 67//请求网络(创建一个主线程,否则数据太多过不来) 68new Thread(new Runnable() { 69 70@Override 71public void run() { 72//定义一个方法,从服务器获取数据。 73String data = https://www.songbingjia.com/android/getDataFromServer(); 74//创建信息对象 75Message message = Message.obtain(); 76Bundle bundle = new Bundle(); 77bundle.putString("data",data); 78message.setData(bundle); 79//设置一个标志: 80message.what = GET_DATA_SUCCESS; 81 82//向主线程发信息 83mHandler.sendMessage(message); 84} 85 86}).start(); 87} 88//从服务器获取数据 89private String getDataFromServer() { 90try{ 91//1.创建一个URL 92URL url = new URL("https://v1.hitokoto.cn/?c=f& encode=text"); 93//2.打开链接 94connection = (HttpURLConnection) url.openConnection(); 95//3.判断并处理结果 96if(connection.getResponseCode()==200){ 97//获取链接的输入流 98inputstream = connection.getInputStream(); 99 100//判断读取的数据 101bufferedreader = new BufferedReader(new InputStreamReader(inputstream)); 102stringBuilder=new StringBuilder(); 103for(String line = ""; (line=bufferedreader.readLine())!=null; ){ 104stringBuilder.append(line); 105} 106return stringBuilder.toString(); 107} 108 109}catch(Exception e){ 110e.printStackTrace(); 111}finally { 112try{ 113if(inputstream!=null)inputstream.close(); 114if(bufferedreader!=null)bufferedreader.close(); 115if(connection!=null)connection.disconnect(); 116}catch(Exception e){ 117e.printStackTrace(); 118} 119} 120 121return ""; 122 123} 124 }

1 < ?xml version="1.0" encoding="utf-8"?> 2 < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3xmlns:tools="http://schemas.android.com/tools" 4android:layout_width="match_parent" 5android:layout_height="match_parent" 6android:paddingBottom="@dimen/activity_vertical_margin" 7android:paddingLeft="@dimen/activity_horizontal_margin" 8android:paddingRight="@dimen/activity_horizontal_margin" 9android:paddingTop="@dimen/activity_vertical_margin" 10tools:context="com.example.hp.app3.MainActivity"> 11 12< TextView 13android:id="@+id/tv_show" 14android:textSize="16dp" 15android:background="@android:color/holo_blue_bright" 16android:layout_width="wrap_content" 17android:layout_height="wrap_content" 18android:text="Hello World!" /> 19 20< Button 21android:id="@+id/btn" 22android:text="刷新数据" 23android:textSize="20dp" 24android:layout_width="match_parent" 25android:layout_height="40dp" 26android:layout_centerVertical="true" 27android:layout_alignParentLeft="true" 28android:layout_alignParentStart="true" /> 29 < /RelativeLayout>

1 < ?xml version="1.0" encoding="utf-8"?> 2 < manifest xmlns:android="http://schemas.android.com/apk/res/android" 3package="com.example.hp.app3"> 4 5//需要添加: 6< uses-permission android:name="android.permission.INTERNET"> 7 8< /uses-permission> 9 10< application 11android:allowBackup="true" 12android:icon="@mipmap/ic_launcher" 13android:label="@string/app_name" 14android:supportsRtl="true" 15android:theme="@style/AppTheme"> 16< activity android:name=".MainActivity"> 17< intent-filter> 18< action android:name="android.intent.action.MAIN" /> 19 20< category android:name="android.intent.category.LAUNCHER" /> 21< /intent-filter> 22< /activity> 23< /application> 24 25 < /manifest>

Android Studio学习路程

文章图片

 
 
Android Studio学习路程

文章图片

 
 
Android Studio学习路程

文章图片

 

    推荐阅读