先添加依赖:
implementation 'com.google.code.gson:gson:2.2.4'
implementation 'com.zhy:okhttputils:2.0.0'
网络权限:
<uses-permission android:name="android.permission.INTERNET"/>
代码实现:
1 public class MainActivity extends AppCompatActivity {
2
3 @Override
4 protected void onCreate(Bundle savedInstanceState) {
5 super.onCreate(savedInstanceState);
6 setContentView(R.layout.activity_main);
7 String url="http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1";
8 OkHttpUtils.get()
9 .url(url)//地址
10 .build()
11 //异步
12 .execute(new StringCallback() {
13 @Override
14 public void onError(Request request, Exception e) {
15 //失败
16 }
17 @Override
18 public void onResponse(String response) {
19 //成功
20 Gson gson = new Gson();
21 Bean bean = gson.fromJson(response, Bean.class);
22 List<Bean.DataBean> data = bean.getData();
23 Toast.makeText(MainActivity.this, data.toString(), Toast.LENGTH_SHORT).show();
24 }
25 });
26 }
27 }