guardar una cadena en la memoria interna y luego mostrarla

hey allí tengo este código que debe guardar un archivo del servidor sql cuando hace clic en el primer botón y luego mostrarlo cuando se presiona el segundo botón. primer botón parece funcionar, pero el segundo botón hace que se bloquee aquí es mi código

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<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 } 

Necesito a alguien que me diga si el código es escribir para guardar a interno es correcto y también por qué el botón 2 se bloquea.

ok, así que este es el código que estoy trabajando fuera

  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() } 

así que cualquiera puede decirme qué códigos no necesito y qué necesito y dónde ponerlo para poder guardar el resultado de story_one.php, y si su autorización para dejarlo como json como im convertirlo más tarde cuando el archivo story_one esta siendo usado

Pruebe este código siguiente para escribir una cadena en el archivo.

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

Pruebe este código para leer el archivo que ha guardado previamente.

 try{ FileInputStream fIn = openFileInput("filename.txt"); InputStreamReader isr = new InputStreamReader(fIn); char[] inputBuffer = new char[len]; //len is the length of that saved string in the file isr.read(inputBuffer); String readString = new String(inputBuffer); }catch(IOException e){ } 
  • Excepción de objetos muertos de Android
  • ¿Cómo utilizar Valgrind con la aplicación android en Eclipse en Ubuntu para encontrar fugas de memoria de código nativo android en tiempo de ejecución?
  • Android - view.Surface OutOfResourcesException
  • ¿Los oyentes crean fugas de memoria si no se eliminan de una actividad destruida?
  • ¿Es esta Runnable a salvo de fugas de memoria?
  • Se pierden las bases de datos de SQLiteConnection al ejecutar el emulador
  • Pasando referencia a la actividad a la clase de utilidad android
  • Referencia débil para la llamada de red mala idea?
  • Error de memoria insuficiente al reiniciar la aplicación (Android)
  • No existe el directorio de caché
  • ¿Por qué Android HCE no admite Mifare Classic?
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.