私はJSON URLからデータを読み込むアプリケーションを持っていますが、ロードに約8秒かかるので、これは解析のためだと思います。
もっと速く簡単に解析する方法があるかどうかを知りたいですか?JSONを解析する最速の方法
これは私が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;
}
}
UPDATE 1:私は何のコードベースを変更
[OK]を人々あなたは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);
}
}
と私のフラグメントクラス:
@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();
http://square.github.io/retrofit/ –
あなたがやっている奇妙な構文解析のほかに、あなたが持っています同期タスクを 'task.execute()。get() 'とします。 –
ジャクソンを解析するためにジャクソンを使用することができます。なぜなら、速くて、ほとんどのベンダーがJaksonをJSONの解析に使用しているからです。 DropBox、Box ..... FYI:http://blog.takipi.com/the-ultimate-json-library-json-simple-vs-gson-vs-jackson-vs-json/ – ViramP