第四课OKGO运用与JSON封装


public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private SeekBar seekBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        seekBar=findViewById(R.id.seekbar);
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest
            .permission.WRITE_EXTERNAL_STORAGE},100);
        }
    }

    public void get(View view) {
        
        OkGo.get("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1").execute(new StringCallback() {
            @Override
            public void onSuccess(Response response) {
                String body = response.body();
                Log.i(TAG, "onSuccess: "+body);
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode==100&&grantResults[0]== PackageManager.PERMISSION_GRANTED){

        }
    }

    public void post(View view) {
//        itemid=2&act=ad_app
        HashMapmap=new HashMap<>();
        map.put("itemid","2");
        map.put("act","ad_app");
        OkGo.post("http://api.yunzhancn.cn/api/app.interface.php?siteid=78703& ").params(map).execute(new StringCallback() {
            @Override
            public void onSuccess(Response response) {
                Toast.makeText(MainActivity.this, ""+response.body(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void getdowload(View view) {
        OkGo.get("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4").execute(new FileCallback("/sdcard/","aaaaa.mp4") {
            @Override
            public void onSuccess(Response response) {
//                File body=response.body();

            }

            @Override
            public void onStart(Request request) {
                super.onStart(request);
            }

            @Override
            public void downloadProgress(Progress progress) {
                super.downloadProgress(progress);
                seekBar.setProgress((int) (progress.fraction*100));
            }

        });

    }

    public void getupload(View view) {
        OkGo.post("http://169.254.9.119/hsf/").isMultipart(true).params("file",new File("/sdcard/Music/早稻叽 - 水星(Cover:TOFUBEATS).mp3"),"aaa.mp3").execute(new StringCallback() {
            @Override
            public void onSuccess(Response response) {
                Toast.makeText(MainActivity.this, ""+response.body(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void getpic(View view) {
        OkGo.get("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=486958716,243120904&fm=26&gp=0.jpg").execute(new BitmapCallback() {
            @Override
            public void onSuccess(Response response) {
                Bitmap body = response.body();
                Toast.makeText(MainActivity.this, ""+body.getByteCount(), Toast.LENGTH_SHORT).show();
            }
        });
    }


    public void json(View view) {
//        "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1"
        OkGo.get("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1").execute(new Jsoncallback(FoodBean.class) {
            @Override
            public void onSuccess(Response response) {
                Log.i(TAG, "onSuccess: "+response);
            }
        });
    }

}

JSON


public abstract class Jsoncallback extends AbsCallback {
   Class tClass;

    public Jsoncallback(Class tClass) {
        this.tClass = tClass;
    }

    @Override
    public T convertResponse(Response response) throws Throwable {
        ResponseBody body = response.body();
        if (body == null) {
            return null;
        }
        String string = body.string();
        T t = new Gson().fromJson(string, tClass);
        return t;
    }
}

你可能感兴趣的:(专高二)