android保存字符串到文件路径,android – 将字符串保存到内部存储器然后显示它...

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

public class MainActivity extends Activity {

/** Called when the activity is first created. */

String result = "";

InputStream is = null;

@Override

public 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

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

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){

}

标签:internal,android,json,memory,save

来源: https://codeday.me/bug/20190726/1539944.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值