Im從json文件加載數據(url)到WebView(使用gson)。當我通過靜態變量(WebView)從不同的Activity加載數據時,我得到了我的JSON文件的第12個URL條目。當im將數據加載到我的Activity的內部WebView時,我得到了第一個像它應該是的URL。從JSON文件中獲取錯誤的數據
OtherActivity.mWebView1.loadUrl(JsonData.getUrl()); --> here i get the 12th url
mWebView2.loadUrl(JsonData.getUrl()); --> here i get the 1st url
它們都使用相同的方法從JSON文件(GSON)加載數據:
@SerializedName("url")
@Expose
private String url;
public String getUrl() {return url;}
public void setUrl(String url) {
this.url = url;
}
這裏我LoadJsonFromAsset類:
public class SwipeUtils {
public static List<AdInfo> loadProfiles(Context context){
try{
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
JSONArray array = new JSONArray(loadJSONFromAsset(context, "adInfo.json"));
List<AdInfo> adInfoList = new ArrayList<>();
adInfoList.indexOf("");
for(int i=0;i<array.length();i++){
AdInfo adInfo = gson.fromJson(array.getString(i), AdInfo.class);
adInfoList.add(adInfo);
Log.d("test",adInfo.toString());
}
return adInfoList;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
private static String loadJSONFromAsset(Context context, String jsonFileName) {
String json = null;
InputStream is=null;
try {
AssetManager manager = context.getAssets();
Log.d(TAG,"path "+jsonFileName);
is = manager.open(jsonFileName);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
}
這是違反規則:/? –