packageorg.lxh.demo;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.PrintStream;importorg.json.JSONArray;importorg.json.JSONException;importorg.json.JSONObject;importandroid.os.Bundle;importandroid.os.Environment;importandroid.app.Activity;public class MainActivity extendsActivity {
@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String data []= new String [] {"www.mldnjava.cn","lixinghua","bbs.mldn.cn"} ;
JSONObject allData= new JSONObject() ;//最外面的节点对象
JSONArray sing = newJSONArray() ;for (int x = 0 ; x < data.length ; x++){
JSONObject temp= new JSONObject() ; //每一个包装的数据都是JSONObject
try{
temp.put("myurl",data[x]);
}catch(JSONException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
sing.put(temp) ;//保存多个JSONObject
}try{
allData.put("urldata", sing) ;
}catch(JSONException e) {//TODO Auto-generated catch block
e.printStackTrace();
}if(!Environment.getExternalStorageState()
.equals(Environment.MEDIA_MOUNTED)){return;
}
File file= newFile(Environment.getExternalStorageDirectory()+File.separator+"mldndata"+File.separator+"json.txt") ;if(!file.getParentFile().exists()){
file.getParentFile().mkdirs() ;
}
PrintStream out= null;try{
out= new PrintStream(newFileOutputStream(file));
out.print(allData.toString()) ;//将数据变为字符串后保存
} catch(FileNotFoundException e) {//TODO Auto-generated catch block
e.printStackTrace();
}finally{if(out != null){
out.close() ;
}
}
}
}