android– 将字符串保存到内部存储器然后显示它

android– 将字符串保存到内部存储器然后显示它,第1张

概述嘿,我有这个代码,当您单击第一个按钮时,应该从sqlserver保存文件,然后在按下第二个按钮时显示它.第一个按钮似乎工作,但第二个按钮导致它崩溃在这里是我的代码publicclassMainActivityextendsActivity{/**Calledwhentheactivityisfirstcreated.*/Stringres

嘿,我有这个代码,当您单击第一个按钮时,应该从sql server保存文件,然后在按下第二个按钮时显示它.第一个按钮似乎工作,但第二个按钮导致它崩溃在这里是我的代码

    public class MainActivity extends Activity {/** Called when the activity is first created. */String result = "";inputStream is = null;@OverrIDepublic voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.main);    button savefile = (button) findVIEwByID(R.ID.downloadBtn);    savefile.setonClickListener(new VIEw.OnClickListener() {        @OverrIDe        public voID onClick(VIEw v) {            returnjson();        }    });    button showfile = (button) findVIEwByID(R.ID.showBtn);    showfile.setonClickListener(new VIEw.OnClickListener() {        @OverrIDe        public voID onClick(VIEw v) {            TextVIEw showtext = (TextVIEw) findVIEwByID(R.ID.showVIEw);        String filename = "Story_One";        String showStorynames = "";        fileinputStream fis = null;        try {            fis = openfileinput(filename);        } catch (fileNotFoundException e1) {            // Todo auto-generated catch block            e1.printstacktrace();        }        try {            fis.read(showStorynames.getBytes());        } catch (IOException e) {            // Todo auto-generated catch block            e.printstacktrace();        }        try {            fis.close();        } catch (IOException e) {            // Todo auto-generated catch block            e.printstacktrace();        }        }    });    //end of onCreate}public voID returnjson(){    try{            httpClIEnt httpClIEnt = new DefaulthttpClIEnt();            httpPost httpPost = new httpPost("http://10.0.2.2/textures_story_List.PHP");            httpResponse response = httpClIEnt.execute(httpPost);            httpentity entity = response.getEntity();            is = entity.getContent();    }catch(Exception e) {        //one.setText("error3");    }    try{        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) {        //one.setText("error2");                    }    try{        JsONArray jArray = new JsONArray(result);        String storynames = "";        for(int i = 0;i<jArray.length();i++){                storynames += jArray.getJsONObject(i).getString("story_name") + "\n";         String filename = "Story_One";        String string = storynames;        fileOutputStream fos = openfileOutput(filename, Context.MODE_PRIVATE);        fos.write(string.getBytes());        fos.close();        }        }    catch(JsONException e) {        //one.setText("error1");    } catch (fileNotFoundException e) {        // Todo auto-generated catch block        e.printstacktrace();    } catch (IOException e) {        // Todo auto-generated catch block        e.printstacktrace();    }        return;//end of returnjson()   }//end of method body}

我需要有人告诉我代码是否写入保存到内部是正确的,以及为什么按钮2崩溃.

好的,这就是我正在努力的代码

    public voID returnjson(){    TextVIEw one = (TextVIEw) findVIEwByID(R.ID.textVIEw1);    try{            httpClIEnt httpClIEnt = new DefaulthttpClIEnt();            httpPost httpPost = new httpPost("http://10.0.2.2/story_one.PHP");            httpResponse response = httpClIEnt.execute(httpPost);            httpentity entity = response.getEntity();            is = entity.getContent();    }catch(Exception e) {        one.setText("error3");    }    try{        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) {        one.setText("error2");                  }    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) {        one.setText("error1");    }        return;//end of returnjson()   }

所以任何人都可以告诉我我不需要什么代码和我需要什么代码以及将它放在哪里以便能够保存story_one.PHP的结果,以及是否可以将其保留为Json,因为我稍后在story_one文件中转换它正在使用

解决方法:

请尝试以下代码将字符串写入文件.

try{String text = "String that goes in the file"fileOutputStream fOut = openfileOutput("filename.txt",MODE_WORLD_READABLE);OutputStreamWriter osw = new OutputStreamWriter(fOut);osw.write(text);int len = text.length();osw.flush();osw.close();}catch(IOException ioe){} 
总结

以上是内存溢出为你收集整理的android – 将字符串保存到内部存储器然后显示它全部内容,希望文章能够帮你解决android – 将字符串保存到内部存储器然后显示它所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1114245.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存