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