Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7513825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:18:19+00:00 2026-05-30T00:18:19+00:00

hey there i have this code that should save a file from sql server

  • 0

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

    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
}

i need someone to tell me if the code is write for saving to internal is right and also why button 2 crashes.

ok so this is the code im working off

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

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:18:20+00:00Added an answer on May 30, 2026 at 12:18 am

    Try this following code to write a string to the file.

    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){
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hey, I have this code that should save a java.util.Vector of custom serializable classes:
hey there guys and girls i have this code that should download a json
Hey. I have this javascript file that I'm getting off the web and it
hey there, i have a div that expands when the page is loaded, now
Hey there, I am trying to get my signout button to work. This is
Hey, I have this code here: ArrayList arrayList = new ArrayList(); arrayList.add(one); arrayList.add(two); arrayList.add(three);
Hey, I just started wondering about this as I came upon a code that
I've always wondered... so you have a code like this: $('#click-me'); and you attach
Hey just a quick question for any experts out there. I have a site
Hey guys, one more quick question for any experts out there. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.