我正在尝试下载我在Firebase存储上上传的JSON文件。我尝试过一些东西,但对我没什么用。
代码null
始终由此doInBackground()
方法返回,导致我的应用无法进一步处理。
protected String doInBackground(String... strings) {
Log.e("Test", "DOINBG Working..");
String storageUrl = "https://firebasestorage.googleapis.com/v0/b/frost-bird-1d290." +
"appspot.com/o/images.json?alt=media&token=b0a511d4-f0ea-4ee0-a063-69b7fd050f49";
FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
StorageReference reference = firebaseStorage.getReferenceFromUrl(storageUrl);
final File localFile;
try {
localFile = File.createTempFile("images", "json");
reference.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(getContext(), "File downloaded successfully.", Toast.LENGTH_SHORT).show();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(localFile)));
StringBuilder result = new StringBuilder();
String line = null;
while((line.concat(String.valueOf(reader.read()))) != null){
Log.e("Test", "Result - "+ result);
result.append(line);
}
finalRes.concat(result.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e("Test", "Failed");
Toast.makeText(getContext(), "File downloading failed.", Toast.LENGTH_LONG).show();
}
});
} catch (IOException e) {
e.printStackTrace();
}
Log.e("Test", "String Downloaded- "+finalRes);
return finalRes;
}
protected void onPostExecute(String result) {
Log.e("Test", "onPostExecute Working.."+"\nString in onPostExecute Method-" +result);
progressDialog.dismiss();
List<String> data = new ArrayList<>();
progressDialog.dismiss();
try{
JSONArray jsonArray = new JSONArray(result);
for(int i=0;i<jsonArray.length();i++){
JSONObject jObject = jsonArray.getJSONObject(i);
data.add(jObject.getString("ipl_image"));
}
recyclerViewGallery = (RecyclerView) getView().findViewById(R.id.recyclerviewgallery);
adapter = new GalleryPhotoAdapter(getContext());
recyclerViewGallery.setAdapter(adapter);
recyclerViewGallery.setLayoutManager(new LinearLayoutManager(getContext()));
}
catch (JSONException e){
Toast.makeText(getContext(),e.toString(), Toast.LENGTH_LONG).show();
}
}
另外我想告诉你它永远不会执行onSuccess()
方法。我不知道为什么。
错误日志:
FATAL EXCEPTION: main Process: com.metrowebz.data.ipl2017, PID: 9526
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
at org.json.JSONTokener.nextValue(JSONTokener.java:94)
at org.json.JSONArray.<init>(JSONArray.java:92)
at org.json.JSONArray.<init>(JSONArray.java:108)
at com.metrowebz.data.ipl2017.NavDFragments.GalleryFragment$AsyncFetch.onPostExecute(GalleryFragment.java:126)
at com.metrowebz.data.ipl2017.NavDFragments.GalleryFragment$AsyncFetch.onPostExecute(GalleryFragment.java:61)
at android.os.AsyncTask.finish(AsyncTask.java:651)
任何人都可以帮我解决这个问题吗?
答案 0 :(得分:0)
您忽略了文件下载的异步性质。成功和失败监听器直到下载完成后才会运行。从Ticket
返回时,成功侦听器尚未运行,因此尚未设置doInBackground()
的值。
因为您在后台线程中执行此处理,所以您可以将代码更改为等待下载完成。像这样:
finalRes