嘿,我有这个代码,当您单击第一个按钮时应该从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<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
}
我需要有人告诉我代码是否写入以保存到内部是正确的以及为什么按钮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<jArray.length();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文件
答案 0 :(得分:5)
尝试使用此代码读取您之前保存的文件。
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){
}
答案 1 :(得分:3)
尝试以下代码将字符串写入文件。
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){
}