package com.example.mywifi_day0609; import android.content.DialogInterface; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Build; import android.provider.Settings; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.telecom.ConnectionService; import android.util.Log; import android.view.View; import android.widget.ListView; import android.widget.Toast; import com.google.gson.Gson; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.List; public class MainActivity extends AppCompatActivity { private ListView listView; private String path="http://v.juhe.cn/joke/content/list.php?sort=0.0&page=&pagesize=&time=1418816972&key=0e1e9432863ab32234c5a22ab5602437"; private List<UserBean.ResultBean.DataBean> data; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView= findViewById(R.id.listview); //得到系统服务 ConnectivityManager manager= (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); //得到网络 NetworkInfo networkInfo = manager.getActiveNetworkInfo(); //判断是够有网 if(networkInfo!=null&&networkInfo.isConnected()){ //得到网络的类型 switch (networkInfo.getType()){ case ConnectivityManager.TYPE_WIFI: Toast.makeText(MainActivity.this,"WIFI", Toast.LENGTH_LONG).show(); break; case ConnectivityManager.TYPE_MOBILE: Toast.makeText(MainActivity.this,"移动数据", Toast.LENGTH_LONG).show(); break; } }else{ AlertDialog.Builder builder= new AlertDialog.Builder(MainActivity.this); builder.setTitle("设置网络"); builder.setMessage("没有网络,你是否设置网络"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { if(Build.VERSION.SDK_INT>10){ Intent intent = null; intent= new Intent(Settings.ACTION_WIRELESS_SETTINGS); startActivity(intent); }else{ Intent intent = new Intent(); intent.setClassName("com.android.settings","com.android.settings.WirelessSettings"); startActivity(intent); } } }); builder.show(); } } public void getdata(View v){ new Thread(){ @Override public void run() { super.run(); try { URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); if(connection.getResponseCode()==200){ InputStream inputStream = connection.getInputStream(); String s = StreamToString(inputStream); Log.i("TAG",s); Gson gson = new Gson(); UserBean userBean = gson.fromJson(s, UserBean.class); data = userBean.getResult().getData(); Log.i("TAG", data.size()+""); //创建适配器 runOnUiThread(new Runnable() { @Override public void run() { Mybase mybase = new Mybase(MainActivity.this, data); listView.setAdapter(mybase); } }); } } catch (IOException e) { e.printStackTrace(); } } }.start(); } private String StreamToString(InputStream inputStream) { InputStreamReader streamReader = new InputStreamReader(inputStream); BufferedReader reader = new BufferedReader(streamReader); String i =null; StringBuilder builder = new StringBuilder(); try { while ((i=reader.readLine())!=null){ builder.append(i); } reader.close();; } catch (Exception e) { e.printStackTrace(); } return builder.toString(); } }
设置网络
最新推荐文章于 2021-07-25 11:20:42 发布