凌空请求太慢
Volley Request too slow
我的应用程序因以下原因崩溃;
loadJsonFromAsset() 方法在使用 update() 方法写入文件后读取 JSON 文件,但它直接进入读取部分,我的应用程序崩溃,因为文件 video.json 不存在,JSON 没有写入,这与 Volley 请求有多慢有关?或者是我如何创建文件?我写入和读取 JSON 文件以将其缓存在用户的 phone 上,如果该文件已经存在于 phone 上,它会直接进入读取部分(但我还没有实现它如果文件存在)。例如,如果我删除代码的读取部分 loadJSONFromAsset() 而不是 运行 update() 方法,JSON 文件将使用 [=28= 创建] 我请求的数据,然后如果我重新 运行 使用 loadJSONFromAsset() 和 bam 的程序,我读取数据并且我的应用程序不 crash.I 认为我的问题是我正在存储缓存慢慢地?
loadJsonFromAsstet() 读取 JSON
public String loadJSONFromAsset() {
update();
String json = null;
try {
FileInputStream is = getActivity().openFileInput("video.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
Toast.makeText(getActivity(), "Error Fetching Data", Toast.LENGTH_LONG).show();
return null;
}
return json;
}
更新();用请求的数据
写入 JSON
public void update(){
// Formulate the request and handle the response.
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, URL, (String )null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Toast.makeText(getActivity(), response.toString(), Toast.LENGTH_LONG).show();
try {
FileOutputStream fos = getActivity().openFileOutput("video.json", Context.MODE_PRIVATE);
Writer out = new OutputStreamWriter(fos);
out.write(response.toString());
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(), "Error Fetching Data", Toast.LENGTH_LONG).show();
}
});
//remove cash
jsonObjectRequest.setShouldCache(false);
mRequestQueue.add(jsonObjectRequest);
}
谢谢!! :)
在确保文件已成功加载和保存之前,您不应尝试读取文件。因此,您需要先调用 update()
方法并在 onResponse
.
中取 json
现在你的 onResponse
在你试图从文件中读取后被调用(因为它是异步的)所以你得到错误。
我的应用程序因以下原因崩溃;
loadJsonFromAsset() 方法在使用 update() 方法写入文件后读取 JSON 文件,但它直接进入读取部分,我的应用程序崩溃,因为文件 video.json 不存在,JSON 没有写入,这与 Volley 请求有多慢有关?或者是我如何创建文件?我写入和读取 JSON 文件以将其缓存在用户的 phone 上,如果该文件已经存在于 phone 上,它会直接进入读取部分(但我还没有实现它如果文件存在)。例如,如果我删除代码的读取部分 loadJSONFromAsset() 而不是 运行 update() 方法,JSON 文件将使用 [=28= 创建] 我请求的数据,然后如果我重新 运行 使用 loadJSONFromAsset() 和 bam 的程序,我读取数据并且我的应用程序不 crash.I 认为我的问题是我正在存储缓存慢慢地?
loadJsonFromAsstet() 读取 JSON
public String loadJSONFromAsset() {
update();
String json = null;
try {
FileInputStream is = getActivity().openFileInput("video.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
Toast.makeText(getActivity(), "Error Fetching Data", Toast.LENGTH_LONG).show();
return null;
}
return json;
}
更新();用请求的数据
写入 JSONpublic void update(){
// Formulate the request and handle the response.
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, URL, (String )null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Toast.makeText(getActivity(), response.toString(), Toast.LENGTH_LONG).show();
try {
FileOutputStream fos = getActivity().openFileOutput("video.json", Context.MODE_PRIVATE);
Writer out = new OutputStreamWriter(fos);
out.write(response.toString());
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(), "Error Fetching Data", Toast.LENGTH_LONG).show();
}
});
//remove cash
jsonObjectRequest.setShouldCache(false);
mRequestQueue.add(jsonObjectRequest);
}
谢谢!! :)
在确保文件已成功加载和保存之前,您不应尝试读取文件。因此,您需要先调用 update()
方法并在 onResponse
.
现在你的 onResponse
在你试图从文件中读取后被调用(因为它是异步的)所以你得到错误。