get方式網路其你去+藍芽狀態判斷+wifi狀態判斷
package com.bawei.bluetoodemo;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TextView tv,tv1;
private WifiManager wm;
private ListView lv;
String str=”藍芽狀態:”;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv= (TextView) findViewById(R.id.tv);
tv1= (TextView) findViewById(R.id.tv1);
lv= (ListView) findViewById(R.id.lv);
//判斷是否有網路
ConnectivityManager cm= (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if(info!=null){
Toast.makeText(MainActivity.this,”當前狀態有網路”,Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,”當前沒有網路連線”,Toast.LENGTH_LONG).show();
} //藍芽狀態 BluetoothAdapter ba=BluetoothAdapter.getDefaultAdapter(); if(ba!=null){ if(ba.isEnabled()){ //為true 開 tv.setText(str+"開"); }else{ //為false 關 tv.setText(str+"關"); } }else{ Toast.makeText(this,"當前不支援藍芽",Toast.LENGTH_LONG).show(); tv.setText(str+"當前狀態不可用"); } //WiFi 狀態 wm= (WifiManager) getSystemService(Context.WIFI_SERVICE); int state = wm.getWifiState(); switch (state){ case 0: tv1.setText("wifi正在關閉"); break; case 1: tv1.setText("wifi已經關閉"); break; case 2: tv1.setText("wifi正在開啟"); break; case 3: tv1.setText("wifi已經開啟"); break; } if(state==3){ //非同步 new AsyncTask<String, Integer, String>() { @Override protected String doInBackground(String... strings) { String json_str=""; //解析介面 try { URL url=new URL("https://www.toutiao.com/api/pc/focus/"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); int code = conn.getResponseCode(); //判斷狀態碼 if(code==200){ InputStream is = conn.getInputStream(); byte arr[]=new byte[1024]; int len=0; while((len=is.read(arr))!=-1){ String s = new String(arr, 0, len); json_str+=s; } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return json_str; } @Override protected void onPostExecute(String s) { // super.onPostExecute(s); //json 字串 json_str Gson gson=new Gson(); User u = gson.fromJson(s, User.class); List<User.DataBean.PcFeedFocusBean> list = u.getData().getPc_feed_focus(); //介面卡 Myadapter adapter=new Myadapter(MainActivity.this,list); lv.setAdapter(adapter); } }.execute(); }else{ Toast.makeText(MainActivity.this,state+"當前無網路連線",Toast.LENGTH_LONG).show(); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
}