2017-03-28 38 views
1

我的問題是保存從JSON文件解析的內容。
我想保存內容,所以從最近清除應用程序後,JSON的內容將保持未刪除狀態。實際上JSON正在被解析,但是在完全關閉一個應用之後,我必須啓用網絡連接來再次檢索JSON的內容。
如果需要更多的代碼,我會添加它 - 只是說這樣做。
乾杯!持久JSON內容

package pl.bugbreaker.cherry56su.Docs; 
import java.util.ArrayList; 
import pl.bugbreaker.cherry56su.Docs.Download_data.download_complete; 
import pl.bugbreaker.cherry56su.R; 

import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.text.util.Linkify; 
import android.view.View; 
import android.widget.ListView; 
import android.widget.TextView; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import static android.support.v7.appcompat.R.styleable.View; 

public class DocsActivity extends AppCompatActivity implements download_complete { 

public ListView docs_list; 
public ArrayList<Doc> documents = new ArrayList<Doc>(); 
public ListAdapter adapter; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_docs); 
    getSupportActionBar().setTitle("Dokumenty"); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    getSupportActionBar().setHomeButtonEnabled(true); 
    getSupportActionBar().setDisplayShowHomeEnabled(true); 
    getSupportActionBar().setElevation(0); 
    docs_list = (ListView) findViewById(R.id.docs_list); 
    adapter = new ListAdapter(this); 
    docs_list.setAdapter(adapter); 

    Download_data download_data = new Download_data((download_complete) this); 
    download_data.download_data_from_link("http://link.com/file.json"); 

} 

public void get_data(String data) 
{ 
    try { 
     JSONArray data_array=new JSONArray(data); 
     for (int i = 0 ; i < data_array.length() ; i++) 
     { 
      JSONObject obj=new JSONObject(data_array.get(i).toString()); 
      Doc add=new Doc(); 
      add.doc = obj.getString("doc"); 
      add.doc_link = obj.getString("doc_link"); 
      documents.add(add); 
     } 
     adapter.notifyDataSetChanged(); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

} 
public boolean onSupportNavigateUp() { 
    onBackPressed(); 
    return true; 
} 

} 

編輯:
我會盡快檢查所有這些答案,當我完成,答案將進行檢查和解決問題的根本。

+0

STOR e'obj.toString()'進入首選項並稍後使用 –

+0

呵呵,但是怎麼樣?使新的活動指定用於存儲?哦,它是'obj.getString()',而不是'.toString()'。我應該使'toString()',然後存儲它? – C0nverter

+0

nopes,一旦你得到你的數據,然後將它存儲到'首選項'並在'onCreate'內部獲取它,如果檢索到的字符串是非空的,然後用它來顯示你的數據 –

回答

2

持久JSON內容

您可以使用GSON更輕鬆地解析JSON數據。 在您的build.gradle文件中添加此依賴項。

compile 'com.google.code.gson:gson:2.8.0' 

然後創建一個POJO類來解析JSON數據。

例POJO類:

public class AppGeneralSettings { 
    @SerializedName("key1") 
String data; 


    public String getData() { 
     return data; 
    } 

} 

樣品JSON對象

{ 
    "key1":"hellow world!" 
} 
  • 爲了解析從因特網使用JSON字符串這個片段

    AppGeneralSettings data=new Gson().fromJson(jsonString, AppGeneralSettings.class); 
    

然後添加一個幫助類來存儲和檢索JSON數據的首選項。

示例:Helper類存儲數據

public class AppPreference { 
    private static final String FILE_NAME = BuildConfig.APPLICATION_ID + ".apppreference"; 
    private static final String APP_GENERAL_SETTINGS = "app_general_settings"; 
    private final SharedPreferences preferences; 

    public AppPreference(Context context) { 
     preferences = context.getSharedPreferences(FILE_NAME, MODE_PRIVATE); 
    } 

    public SharedPreferences.Editor setGeneralSettings(AppGeneralSettings appGeneralSettings) { 
     return preferences.edit().putString(APP_GENERAL_SETTINGS, new Gson().toJson(appGeneralSettings)); 
    } 

    public AppGeneralSettings getGeneralSettings() { 
     return new Gson().fromJson(preferences.getString(APP_GENERAL_SETTINGS, "{}"), AppGeneralSettings.class); 
    } 
} 

要保存數據

new AppPreference().setGeneralSettings(appGeneralSettings).commit(); 

要檢索數據

AppGeneralSettings appGeneralSettings = new AppPreference().getGeneralSettings(); 
+0

你能告訴我,究竟是什麼「key1」?這只是像偏好文件中的'android:key =「key1」'嗎? – C0nverter

+0

它只是json字符串中使用的自定義名稱 – Darish