Note that there are some explanatory texts on larger screens.

plurals
  1. POsaving a string to internal memory then displaying it
    primarykey
    data
    text
    <p>hey there i have this code that should save a file from sql server when you click the first button, and then display it when the second button is pressed. first button seems to work but the second button causes it to crash here is my code</p> <pre><code> 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&lt;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 } </code></pre> <p>i need someone to tell me if the code is write for saving to internal is right and also why button 2 crashes. </p> <p>ok so this is the code im working off</p> <pre><code> 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&lt;jArray.length();i++){ storyNames += jArray.getJSONObject(i).getString("story_name") + "\n"; } one.setText(storyNames); } catch(JSONException e) { one.setText("error1"); } return; //end of returnJson() } </code></pre> <p>so can anyone tell me what codes i dont need and what i do need and where to put it to be able to save the result from story_one.php, and whether its ok to leave it as json as im converting it later when the story_one file is being used</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload