package com.bwie.day4_httppost_demo03_1; import android.content.ComponentName; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Path; import android.media.audiofx.BassBoost; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.AsyncTask; 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.view.View; import android.widget.ListView; import android.widget.TextView; import com.google.gson.Gson; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; public class MainActivity extends AppCompatActivity { ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.lv); findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); NetworkInfo info = manager.getActiveNetworkInfo(); if (info != null && info.isAvailable()) { MyTask task = new MyTask(); try { String path = "http://apis.juhe.cn/cook/query.php"; String value = "menu=" + URLEncoder.encode("土豆", "utf-8") + "&key=13af589c334ec80c037688e927407966&rn=1"; //开启异步任务,传入一个string数组,第一个是网络地址,第二个是参数; task.execute(new String[]{path, value}); } catch (Exception e) { e.printStackTrace(); } } else { //弹出提醒网络未开启,跳转开启网络界面 showDialog(); } } }); } private void showDialog() { 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 = new Intent(Settings.ACTION_WIRELESS_SETTINGS); startActivity(intent); } else { Intent intent = new Intent(); ComponentName name = new ComponentName("com.android.settings", "com.android.settings.WIRELESS_SETTINGS"); intent.setComponent(name); intent.setAction(Intent.ACTION_VIEW); startActivity(intent); } dialogInterface.dismiss(); } }); builder.setNegativeButton("不开启", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.dismiss(); } }); builder.create().show(); } class MyTask extends AsyncTask<String, Integer, DataBean> { @Override protected DataBean doInBackground(String... strings) { String json = getNetData(strings[0], strings[1]); DataBean dataBean = new Gson().fromJson(json, DataBean.class); return dataBean; } @Override protected void onPostExecute(DataBean dataBean) { super.onPostExecute(dataBean); MyAdapter adapter = new MyAdapter(dataBean.result.data.get(0).steps, MainActivity.this); lv.setAdapter(adapter); } } private String getNetData(String path, String value) { //获取URL的对象 try { URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoInput(true); //写入参数 OutputStream outputStream = connection.getOutputStream(); outputStream.write(value.getBytes()); //获取网络请求状态码 int code = connection.getResponseCode(); //200,连接成功 if (code == HttpURLConnection.HTTP_OK) { //获取网络请求返回的输入流 InputStream is = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String str = ""; StringBuilder builder = new StringBuilder(); while ((str = reader.readLine()) != null) { builder.append(str); } return builder.toString(); } } catch (Exception e) { e.printStackTrace(); } return null; } }
异步任务,开启网络的dialog , 解析网络上的json
最新推荐文章于 2017-08-09 14:31:07 发布