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

saving a json file to internal memory then calling that file later(将 json 文件保存到内部存储器,然后稍后调用该文件)
本文介绍了将 json 文件保存到内部存储器,然后稍后调用该文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a json file I'm trying to save when btn one is clicked and then another btn that will display the decoded json file in a textview

    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 + "
");
            }
            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") + "
"; 
        }
        one.setText(storyNames);
    }
    catch(JSONException e) {
    }
        return;


//end of returnJson()   
}



//end of class body    
}

它在启动时崩溃_

runtimeExecption unable to instatiate activity ComponentInfo nullpointer

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

oK that problem fixed here is the codes I added to save and then show the file

    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 + "
");
            }
            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") + "
"; 
        }
        TextView one = (TextView) findViewById(R.id.showView);

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


//end of returnJson()   
}



//end of class body    
}

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

Can someone help me fix the errors or tell me what I'm doing wrong?

推荐答案

两个建议:

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

1) Move this assignment into your onCreate() method:

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

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

2) If you still have the problem, then step through your code to determine exactly where you're trying to dereference a null pointer. I'm guessing it's probably one of your "findViewById()" calls.

这篇关于将 json 文件保存到内部存储器,然后稍后调用该文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)