嘿,我有一个代码,当你点击第一个按钮时,它应该从SQL服务器保存文件,然后在按下第二个按钮时显示文件。第一个按钮似乎有效,但第二个按钮会导致它崩溃。这是我的代码:
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
}
我希望你能帮我确认保存到内部存储的代码是否正确,以及为什么第二个按钮会导致崩溃问题。
好的,这是我正在使用的代码:
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文件使用时再进行转换?