public class MainActivity extends AppCompatActivity { ListView lv; Button btn; TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.lv); btn = (Button) findViewById(R.id.btn); tv = (TextView) findViewById(R.id.tv); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { 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"; task.execute(new String[]{path,value}); } catch (Exception e) { e.printStackTrace(); } } else { showDialog(); } } }); } class MyTask extends AsyncTask<String,Integer,DataBean>{ @Override protected DataBean doInBackground(String... params) { String json = getData(params[0], params[1]); Gson gson = new Gson(); DataBean bean = gson.fromJson(json, DataBean.class); return bean; } @Override protected void onPostExecute(DataBean dataBean) { super.onPostExecute(dataBean); // tv.setText(dataBean.result.data.get(0).steps.toString()); MyAdapter adapter = new MyAdapter(dataBean.result.data.get(0).steps,MainActivity.this); lv.setAdapter(adapter); } } private String getData(String path, String value) { try { URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); OutputStream outputStream = connection.getOutputStream(); outputStream.write(value.getBytes()); if(connection.getResponseCode() == 200){ InputStream inputStream = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String str = ""; StringBuilder builder = new StringBuilder(); while((str = reader.readLine()) != null){ builder.append(str); } return builder.toString(); } } catch (Exception e) { e.printStackTrace(); } return null; } private void showDialog() { final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("网络状态提醒"); builder.setMessage("是否开启"); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (Build.VERSION.SDK_INT > 10) { Intent in = new Intent(Settings.ACTION_WIRELESS_SETTINGS); startActivity(in); } else { Intent intent = new Intent(); ComponentName component = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings"); intent.setComponent(component); intent.setAction("android.intent.action.VIEW"); } dialog.dismiss(); } }); builder.setNegativeButton("NO", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } }
网络判断大爱log 加 POST请求
最新推荐文章于 2020-08-15 00:04:02 发布