public class MainActivity extends AppCompatActivity {
ListView lv;
Button btn;
@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);
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()){
Task task = new Task();
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 Task extends AsyncTask<String,Integer,DataBean>{
@Override
protected DataBean doInBackground(String... params) {
String json = getNateData(params[0],params[1]);
DataBean bean = new Gson().fromJson(json,DataBean.class);
return bean;
}
@Override
protected void onPostExecute(DataBean dataBean) {
super.onPostExecute(dataBean);
MyAdapter adapter = new MyAdapter(MainActivity.this,dataBean.result.data.get(0).steps);
lv.setAdapter(adapter);
}
}
public String getNateData(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());
//请求网络状态码
int code = connection.getResponseCode();
Log.d( " code"," "+code);
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;
}
public void showDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("网络状态提醒");
builder.setMessage("您的网络未开启,是否开启网络");
builder.setPositiveButton("开启网络", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
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);
}
dialog.dismiss();
}
});
builder.setNegativeButton("不启动",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}
}
ListView lv;
Button btn;
@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);
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()){
Task task = new Task();
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 Task extends AsyncTask<String,Integer,DataBean>{
@Override
protected DataBean doInBackground(String... params) {
String json = getNateData(params[0],params[1]);
DataBean bean = new Gson().fromJson(json,DataBean.class);
return bean;
}
@Override
protected void onPostExecute(DataBean dataBean) {
super.onPostExecute(dataBean);
MyAdapter adapter = new MyAdapter(MainActivity.this,dataBean.result.data.get(0).steps);
lv.setAdapter(adapter);
}
}
public String getNateData(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());
//请求网络状态码
int code = connection.getResponseCode();
Log.d( " code"," "+code);
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;
}
public void showDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("网络状态提醒");
builder.setMessage("您的网络未开启,是否开启网络");
builder.setPositiveButton("开启网络", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
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);
}
dialog.dismiss();
}
});
builder.setNegativeButton("不启动",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}
}