将 json 文件保存到内部存储器,然后稍后调用该文件

Posted

技术标签:

【中文标题】将 json 文件保存到内部存储器,然后稍后调用该文件【英文标题】:saving a json file to internal memory then calling that file later 【发布时间】:2012-02-19 06:06:25 【问题描述】:

我有一个 json 文件,我试图在单击 btn 时保存一个,然后另一个 btn 将在 textview 中显示解码的 json 文件

    public class MainActivity extends Activity 
/** Called when the activity is first created. */

String result = "";
InputStream is = null;
TextView one = (TextView) findViewById(R.id.showView);
HttpEntity entity = null;

@Override
public void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    //pulls a file from my server then saves it
    Button btn1 = (Button) findViewById(R.id.downloadBtn);
    btn1.setOnClickListener(new View.OnClickListener() 

        @Override
        public void onClick(View v) 


            saveJson();

        //end of onClick    
        
    //end of onClickListener
    );

    //should show the saved file
    Button btn2 = (Button) findViewById(R.id.showBtn);
    btn2.setOnClickListener(new View.OnClickListener() 

        @Override
        public void onClick(View v) 

            showJson();

        //end of onClick    
        
    //end of onClickListener
    );

//end of oncreate()    


public void saveJson()

    TextView one = (TextView) findViewById(R.id.showView);

    try
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://10.0.2.2/textures_story_list.php");

            HttpResponse response = httpClient.execute(httpPost);
            entity = response.getEntity();
//from here what do i need to add to save the file as .json
    catch(Exception e) 
        one.setText("error3");
    


//end of returnJson()   


public void showJson()

    //from here what should i have to show the file story_list.json

try



        is = entity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"), 8);                      
        StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) 
                sb.append(line + "\n");
            
            is.close();
            result = sb.toString();

    catch(Exception e) 
        

    try
        JSONArray jArray = new JSONArray(result);
        String storyNames = "";
        for(int i = 0;i<jArray.length();i++)
                storyNames += jArray.getJSONObject(i).getString("story_name") + "\n"; 
        
        one.setText(storyNames);
    
    catch(JSONException e) 
    
        return;


//end of returnJson()   




//end of class body    

它在启动时崩溃_

runtimeExecption unable to instatiate activity ComponentInfo nullpointer

好的,这里解决的问题是我添加的代码以保存然后显示文件

    public class MainActivity extends Activity 
/** Called when the activity is first created. */

String result = "";
InputStream is = null;
HttpEntity entity = null;
HttpEntity readString = null;
@Override
public void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    //pulls a file from my server then saves it
    Button btn1 = (Button) findViewById(R.id.downloadBtn);
    btn1.setOnClickListener(new View.OnClickListener() 

        @Override
        public void onClick(View v) 


            saveJson();

        //end of onClick    
        
    //end of onClickListener
    );

    //should show the saved file
    Button btn2 = (Button) findViewById(R.id.showBtn);
    btn2.setOnClickListener(new View.OnClickListener() 

        @Override
        public void onClick(View v) 

            showJson();

        //end of onClick    
        
    //end of onClickListener
    );

//end of oncreate()    


public void saveJson()

    TextView one = (TextView) findViewById(R.id.showView);

    try
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://10.0.2.2/textures_story_list.php");

            HttpResponse response = httpClient.execute(httpPost);
            entity = response.getEntity();
//from here what do i need to add to save the file as .json
    catch(Exception e) 
        one.setText("error3");
    
    try

        HttpEntity text = entity;
        FileOutputStream fOut = openFileOutput("story.json",MODE_WORLD_READABLE);
        OutputStreamWriter osw = new OutputStreamWriter(fOut);
        osw.write(text);
        osw.flush();
        osw.close();
        catch(IOException ioe)
           

//end of returnJson()   


public void showJson()

    //from here what should i have to show the file story_list.json

    try
        FileInputStream fIn = openFileInput("story.json");
        InputStreamReader isr = new InputStreamReader(fIn);
        char[] inputBuffer = new char[11];
        //len is the length of that saved string in the file

        isr.read(inputBuffer);

       // String readString = new String(inputBuffer);





        isr = readString.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"), 8);                      
        StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) 
                sb.append(line + "\n");
            
            is.close();
            result = sb.toString();

    catch(Exception e) 
        

    try
        JSONArray jArray = new JSONArray(result);
        String storyNames = "";
        for(int i = 0;i<jArray.length();i++)
                storyNames += jArray.getJSONObject(i).getString("story_name") + "\n"; 
        
        TextView one = (TextView) findViewById(R.id.showView);

        one.setText(storyNames);
    
    catch(JSONException e) 
    
        return;


//end of returnJson()   




//end of class body    

有人可以帮我解决错误或告诉我我做错了什么吗?

【问题讨论】:

嗨 - 你的第一个问题是访问冲突 - 你说你解决了它。酷 :) 一切正常吗?如果不是,请具体说明问题所在。如果可能,剪切/粘贴错误消息。告诉我们哪条具体的线路失败了——以及它是如何失败的——当你逐步调试调试器时。如果您有任何问题 - 请不要让我们猜测。准确指定什么/在哪里。好吗? 【参考方案1】:

两个建议:

1) 将此分配移动到您的 onCreate() 方法中:

one = (TextView) findViewById(R.id.showView);

2) 如果您仍然有问题,请逐步检查您的代码以确定您尝试取消引用空指针的确切位置。我猜这可能是您的“findViewById()”调用之一。

【讨论】:

iv 修复了这个问题,但仍然需要知道如何输入代码以保存和显示 json 文件

以上是关于将 json 文件保存到内部存储器,然后稍后调用该文件的主要内容,如果未能解决你的问题,请参考以下文章

使用 php 将 PDF 文件保存到 XML 文件

将文件下载到内部存储 [重复]

将字符串保存到内部存储上的特定文件夹 - Android

如何将位图保存在从 Internet 下载的内部存储中

需要将表格视图的内容保存为 txt 文件并稍后重新加载

将函数存储在列表中并稍后调用它们