标签:mtv iss style owb handler new ace nbsp nta
今天学习怎样调用互联网的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 { 22 private int GET_DATA_SUCCESS = 101;//获取信息成功的标志 23 private TextView mTvShow; 24 private BufferedReader bufferedreader; 25 private InputStream inputstream; 26 private StringBuilder stringBuilder; 27 private HttpURLConnection connection; 28 29 //多线程传输数据 Handler 30 Handler mHandler = new Handler(new Handler.Callback() { 31 @Override 32 public boolean handleMessage(Message message) { 33 if(message.what==GET_DATA_SUCCESS){ 34 String data = message.getData().getString("data"); 35 Log.i("MainActivity",data); 36 mTvShow.setText(data); 37 } 38 return false; 39 } 40 }); 41 42 @Override 43 protected void onCreate(Bundle savedInstanceState) { 44 super.onCreate(savedInstanceState); 45 setContentView(R.layout.activity_main); 46 47 //初始化控件 48 initUI(); 49 //初始化数据 50 initData(); 51 } 52 53 private void initUI() { 54 //获取文本框 55 mTvShow = (TextView) findViewById(R.id.tv_show); 56 //获取按钮 57 findViewById(R.id.btn).setOnClickListener(this); 58 } 59 60 @Override 61 public void onClick(View view) { 62 //初始化数据 63 initData(); 64 } 65 66 private void initData() { 67 //请求网络(创建一个主线程,否则数据太多过不来) 68 new Thread(new Runnable() { 69 70 @Override 71 public void run() { 72 //定义一个方法,从服务器获取数据。 73 String data = getDataFromServer(); 74 //创建信息对象 75 Message message = Message.obtain(); 76 Bundle bundle = new Bundle(); 77 bundle.putString("data",data); 78 message.setData(bundle); 79 //设置一个标志: 80 message.what = GET_DATA_SUCCESS; 81 82 //向主线程发信息 83 mHandler.sendMessage(message); 84 } 85 86 }).start(); 87 } 88 //从服务器获取数据 89 private String getDataFromServer() { 90 try{ 91 //1.创建一个URL 92 URL url = new URL("https://v1.hitokoto.cn/?c=f&encode=text"); 93 //2.打开链接 94 connection = (HttpURLConnection) url.openConnection(); 95 //3.判断并处理结果 96 if(connection.getResponseCode()==200){ 97 //获取链接的输入流 98 inputstream = connection.getInputStream(); 99 100 //判断读取的数据 101 bufferedreader = new BufferedReader(new InputStreamReader(inputstream)); 102 stringBuilder=new StringBuilder(); 103 for(String line = "";(line=bufferedreader.readLine())!=null;){ 104 stringBuilder.append(line); 105 } 106 return stringBuilder.toString(); 107 } 108 109 }catch(Exception e){ 110 e.printStackTrace(); 111 }finally { 112 try{ 113 if(inputstream!=null)inputstream.close(); 114 if(bufferedreader!=null)bufferedreader.close(); 115 if(connection!=null)connection.disconnect(); 116 }catch(Exception e){ 117 e.printStackTrace(); 118 } 119 } 120 121 return ""; 122 123 } 124 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.example.hp.app3.MainActivity"> 11 12 <TextView 13 android:id="@+id/tv_show" 14 android:textSize="16dp" 15 android:background="@android:color/holo_blue_bright" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:text="Hello World!" /> 19 20 <Button 21 android:id="@+id/btn" 22 android:text=" 刷新数据" 23 android:textSize="20dp" 24 android:layout_width="match_parent" 25 android:layout_height="40dp" 26 android:layout_centerVertical="true" 27 android:layout_alignParentLeft="true" 28 android:layout_alignParentStart="true" /> 29 </RelativeLayout>
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.hp.app3"> 4 5 //需要添加: 6 <uses-permission android:name="android.permission.INTERNET"> 7 8 </uses-permission> 9 10 <application 11 android:allowBackup="true" 12 android:icon="@mipmap/ic_launcher" 13 android:label="@string/app_name" 14 android:supportsRtl="true" 15 android: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>
标签:mtv iss style owb handler new ace nbsp nta
原文地址:https://www.cnblogs.com/mxk123456/p/12309685.html