Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用改型下载URL_Android_Retrofit - Fatal编程技术网

Android 使用改型下载URL

Android 使用改型下载URL,android,retrofit,Android,Retrofit,我正在尝试下载我在使用改装时得到的响应,但我得到了错误: Failed to invoke public com.squareup.okhttp.ResponseBody() with no args 我不明白问题出在哪里 我有一个recyclerView,每个项目都有一个单独的URL 这是我的适配器类,带有带有保存选项的弹出菜单: popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

我正在尝试下载我在使用改装时得到的响应,但我得到了错误:

Failed to invoke public com.squareup.okhttp.ResponseBody() with no args
我不明白问题出在哪里

我有一个recyclerView,每个项目都有一个单独的URL

这是我的适配器类,带有带有保存选项的弹出菜单:

popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){

                    case R.id.save:

                        final ApiInterface  downloadService = ApiClient.getClient().create(ApiInterface.class);


                        Call<ResponseBody> call = downloadService.downloadFileWithDynamicUrlSync(url);
                        call.enqueue(new Callback<ResponseBody>() {
                            @Override
                            public void onResponse(Call<ResponseBody> call, final Response<ResponseBody> response) {
                                if (response.isSuccessful()) {
                                    Log.d("servercontact", "server contacted and has file");

                                    new AsyncTask<Void, Void, Void>() {
                                        @Override
                                        protected Void doInBackground(Void... voids) {
                                            boolean writtenToDisk = writeResponseBodyToDisk( response.body());

                                            Log.d("downloadsuccess", "file download was a success? " + writtenToDisk);
                                            return null;
                                        }
                                    }.execute();
                                }
                                else {
                                    Log.d("failedserver", "server contact failed");
                                }
                            }

                            @Override
                            public void onFailure(Call<ResponseBody> call, Throwable t) {
                                Log.e("error", t.toString());
                            }
                        });
                        break;
                }
                return true;
            }
        });
        popup.show();



private boolean writeResponseBodyToDisk(ResponseBody body) {
        try {
            // todo change the file location/name according to your needs
            File futureStudioIconFile = new File(Environment.DIRECTORY_DOWNLOADS + File.separator + "Future Studio Icon.png");

            InputStream inputStream = null;
            OutputStream outputStream = null;

            try {
                byte[] fileReader = new byte[4096];

                long fileSize = body.contentLength();
                long fileSizeDownloaded = 0;

                inputStream = body.byteStream();
                outputStream = new FileOutputStream(futureStudioIconFile);

                while (true) {
                    int read = inputStream.read(fileReader);

                    if (read == -1) {
                        break;
                    }

                    outputStream.write(fileReader, 0, read);

                    fileSizeDownloaded += read;

                    Log.d("filedownload", "file download: " + fileSizeDownloaded + " of " + fileSize);
                }

                outputStream.flush();

                return true;
            } catch (IOException e) {
                return false;
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }

                if (outputStream != null) {
                    outputStream.close();
                }
            }
        } catch (IOException e) {
            return false;
        }
    }
popup.setOnMenuItemClickListener(新的PopupMenu.OnMenuItemClickListener(){
@凌驾
公共布尔onMenuItemClick(菜单项){
开关(item.getItemId()){
案例R.id.save:
最终ApiInterface downloadService=ApiClient.getClient().create(ApiInterface.class);
Call Call=downloadService.downloadfilewithdynamiccurlsync(url);
call.enqueue(新回调(){
@凌驾
公共void onResponse(呼叫、最终响应){
if(response.issusccessful()){
Log.d(“服务器联系人”,“服务器已联系并具有文件”);
新建异步任务(){
@凌驾
受保护的空位背景(空位…空位){
布尔writentodisk=writeResponseBodyToDisk(response.body());
Log.d(“下载成功”,“文件下载成功?”+writentodisk);
返回null;
}
}.execute();
}
否则{
Log.d(“failedserver”、“服务器联系失败”);
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.e(“error”,t.toString());
}
});
打破
}
返回true;
}
});
popup.show();
专用布尔writeResponseBodyToDisk(响应主体){
试一试{
//todo根据需要更改文件位置/名称
File futureStudioIconFile=新文件(Environment.DIRECTORY_DOWNLOADS+File.separator+“Future Studio Icon.png”);
InputStream InputStream=null;
OutputStream OutputStream=null;
试一试{
字节[]文件读取器=新字节[4096];
long fileSize=body.contentLength();
long FILESIZEDOWNLOAD=0;
inputStream=body.ByTestStream();
outputStream=新文件outputStream(FutureStudioConfigile);
while(true){
int read=inputStream.read(文件读取器);
如果(读取==-1){
打破
}
write(fileReader,0,read);
fileSizeDownloaded+=读取;
Log.d(“文件下载”,“文件下载:“+filesizedownload+”of“+fileSize”);
}
outputStream.flush();
返回true;
}捕获(IOE异常){
返回false;
}最后{
如果(inputStream!=null){
inputStream.close();
}
if(outputStream!=null){
outputStream.close();
}
}
}捕获(IOE异常){
返回false;
}
}
以下是我的改装接口类:

 @Streaming
    @GET
    Call<ResponseBody> downloadFileWithDynamicUrlSync(@Url String fileUrl);

public class ApiClient {

    public static final String Base_Url = "https://newsapi.org/v1/";
    public static Retrofit retrofit = null;

    static Gson gson = new GsonBuilder()
            .setLenient()
            .create();

    public static Retrofit getClient(){
        if(retrofit==null){
            retrofit= new Retrofit.Builder()
                    .baseUrl(Base_Url)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .build();
        }

        return retrofit;
    }
}
@流媒体
@得到
调用DownloadFileWithDynamicCurlSync(@Url字符串fileUrl);
公共类客户端{
公共静态最终字符串Base_Url=”https://newsapi.org/v1/";
公共静态改装=空;
静态Gson Gson=new GsonBuilder()
.setLenient()
.create();
公共静态更新getClient(){
如果(改装==null){
改装=新改装.Builder()
.baseUrl(基本Url)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
回流改造;
}
}

我想下载URL,以便用户可以脱机查看。

您需要使用OkHttp3.x中的
okhttp3.responseBooky
(改型取决于)。该错误消息表示您正在使用OkHttp2.x中的类型