其他分享
首页 > 其他分享> > 大二下学期团队项目(app端与服务器端数据交互)

大二下学期团队项目(app端与服务器端数据交互)

作者:互联网

今日主要学习了app端与服务端进行数据交互。

主要利用xutils3来实现网络传输,需要引入xUtils3,在build.gradle中添加

compile 'org.xutils:xutils:3.5.0'

1.下载图片,视频,音乐等

public void downloadFile(){
//请求的地址 RequestParams params = new RequestParams("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farticle%2Fca3c577fca3ed067015ba144d11c4bcdd61348c4.jpg&refer=http%3A%2F%2Fi0.hdslb.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1621306046&t=66837f7202d0d974bd0be653b2754dea"); //要保存的位置
params.setSaveFilePath(Environment.getExternalStorageDirectory()+"/test_download/YLY.jpg");
// params.setCancelFast(true); params.setAutoRename(true);
//断点续传 params.setAutoResume(true);
//线程池,可以加快下载速度 params.setExecutor(new PriorityExecutor(3,true));
//带进度条的下载 x.http().get(params, new Callback.ProgressCallback<File>() { @Override public void onWaiting() { Log.e("TAG","onWaiting"); } @Override public void onStarted() { Log.e("TAG","onStarted"); } @Override public void onl oading(long total, long current, boolean isDownloading) { progressbar.setMax((int)total); progressbar.setProgress((int)current); Log.e("TAG","onLoading=="+current+"/"+total+",isDownloading=="+isDownloading); } @Override public void onSuccess(File result) { Log.e("TAG","onSuccess=="+result.toString()); Toast.makeText(xUtils3TestActivity.this,"下载完成",Toast.LENGTH_LONG).show(); } @Override public void one rror(Throwable ex, boolean isOnCallback) { Log.e("TAG","onError=="+ex.toString()); } @Override public void onCancelled(CancelledException cex) { Log.e("TAG","onCancelled=="+cex.toString()); } @Override public void onFinished() { Log.e("TAG","onFinished"); } }); }

2.上传数据到服务器

RequestParams params = new RequestParams("http://10.92.5.115:8080/BuyIII/BusinessManServlet");
        //params.setMultipart(true);
        params.addBodyParameter("itemid","1234");
        params.addBodyParameter("itemname","写入数据库测试");
        params.addBodyParameter("itemprice","1000");
        params.addBodyParameter("itemnum","1000");
        //params.addBodyParameter("File",new File(Environment.getExternalStorageDirectory()+"/test_download/YLY.jpg"),null,"YLY.jpg");
        x.http().post(params, new Callback.CacheCallback<String>() {

            @Override
            public boolean onCache(String result) {
                return false;
            }

            @Override
            public void onSuccess(String result) {
                Toast.makeText(xUtils3TestActivity.this,"上传完成",Toast.LENGTH_LONG).show();
            }

            @Override
            public void one rror(Throwable ex, boolean isOnCallback) {
                Log.e("TAG","onError=="+ex.toString());
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Log.e("TAG","onCancelled=="+cex.toString());
            }

            @Override
            public void onFinished() {
                Log.e("TAG","onFinished");
            }
        });

跟下载很类似,addBodyParameter添加要传输的数据,可以为文件或字符数据,Callback.CacheCallback<String>()这里要做相应的改变,并注意改为post请求。

标签:服务器端,下学期,void,public,TAG,params,Override,app,Log
来源: https://www.cnblogs.com/fengchuiguobanxia/p/14674652.html