我有一个从JSON URL加载数据的应用程序,但加载大约需要8秒钟,我相信这是因为解析。
我想知道是否有更快更轻松地解析它的方法?
这是我用来阅读JSON的函数:
public class LoadJson extends AsyncTask <String, Void, String> {
@Override
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String finalJson = buffer.toString();
return finalJson;
} catch (Exception e) {
e.printStackTrace();
return "Faild";
}
}
}
和
public JSONArray ExcuteLoad() {
LoadJson task = new LoadJson();
String resualt = null;
try {
resualt = task.execute("MY_JSON_FILE_URL").get();
JSONObject json = new JSONObject(resualt);
JSONArray jarray = json.getJSONArray("marcadores");
return jarray;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
更新1:
好的人我根据你建议使用onPostExecute
改变代码,但问题是我无法在asyncTask之外返回jsonarray的值,真的很困惑....
public class LoadJson extends AsyncTask <String, Void, String> {
public class LoadJson extends AsyncTask <String, Void, String> {
public interface AsyncResponse {
void processFinish(String output);
}
public AsyncResponse delegate = null;
public LoadJson (AsyncResponse delegate){
this.delegate = delegate;
}
@Override
protected String doInBackground(String... params) {
String resualt = "";
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(params[0]);
urlConnection = (HttpURLConnection)url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while (data != -1) {
char current = (char) data;
resualt += current;
data = reader.read();
}
return resualt;
}
catch (Exception e) {
e.printStackTrace();
return "Failed";
}
}
@Override
protected void onPostExecute(String result) {
delegate.processFinish(result);
}
}
和My Fragment类:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
LoadJson asyncTask = (LoadJson) new LoadJson (new LoadJson.AsyncResponse(){
@Override
public void processFinish(String output){
//Here you will receive the result fired from async class
//of onPostExecute(result) method.
try {
JSONObject jsonObject = new JSONObject(output);
JSONArray jsonArray = jsonObject.getJSONArray("marcadores");
} catch (JSONException e) {
e.printStackTrace();
}
}
}).execute();
答案 0 :(得分:1)
您的问题是没有解析JSON。你不能加快速度。使用不同的库(可能)也不会让它更快。 (我说的是加载时间,而不是开发时间)。
归结为您如何提出请求以及您的网速。
例如,这不是您使用AsyncTask的方式。
resualt = task.execute("MY_JSON_FILE_URL").get();
因为您刚刚将异步调用转换为同步调用。换句话说,get()
方法将阻塞并等待结果,因此需要花费时间并导致数据加载缓慢。
现在,确定,库降低了AsyncTask的复杂性并使开发更快更容易,但这里的快速答案是实际使用AsyncTask类的onPostExecute
来异步加载数据,脱离主线程。
我能给出的最好的例子是Using a callback to return the data
更新
private JSONArray array;
private void parseJSON(JSONArray array) {
this.array = array;
// TODO: something
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
LoadJson asyncTask = (LoadJson) new LoadJson (new LoadJson.AsyncResponse(){
@Override
public void processFinish(String output){
//Here you will receive the result fired from async class
//of onPostExecute(result) method.
try {
JSONObject jsonObject = new JSONObject(output);
JSONArray jsonArray = jsonObject.getJSONArray("marcadores");
for (int i = 0; i < jsonArray.length; i++) {
// TODO: Parse the array, fill an arraylist
}
// TODO: Set / notify an adapter
// Or....
parseJSON(jsonArray);
} catch (JSONException e) {
e.printStackTrace();
}
}
}).execute();