如何在Android应用程序上从外部存储解析或读取json文件
我目前实现了从服务器(url)解析json。但是我找不到从sdcard(/Download/example.json)解析json的方法。有人能帮我解决这个问题/更改这个代码吗 我用asyncTask来完成这个任务。示例教程或示例代码更受欢迎。(对不起,我的英语不好。)如何在Android应用程序上从外部存储解析或读取json文件,android,json,android-layout,getjson,android-4.2-jelly-bean,Android,Json,Android Layout,Getjson,Android 4.2 Jelly Bean,我目前实现了从服务器(url)解析json。但是我找不到从sdcard(/Download/example.json)解析json的方法。有人能帮我解决这个问题/更改这个代码吗 我用asyncTask来完成这个任务。示例教程或示例代码更受欢迎。(对不起,我的英语不好。) public类主扩展活动{ 私人文本视图商店显示; 专用静态字符串searchURL=”http://example.com/sample.json"; @凌驾 创建时受保护的void(Bundle savedInstanceS
public类主扩展活动{
私人文本视图商店显示;
专用静态字符串searchURL=”http://example.com/sample.json";
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.baby);
//全班参考
shopsDisplay=(TextView)findviewbyd(R.id.tweet_-txt);
新建GetShops().execute(searchURL);
}
私有类GetShops扩展异步任务{
/*
*在后台执行抓取任务
*-通过执行方法接收搜索URL
*/
@凌驾
受保护的字符串背景(字符串…shopsURL){
//开始生成结果,该结果将是json字符串
StringBuilder shopsFeedBuilder=新建StringBuilder();
//应仅为一个URL,接收数组
for(字符串搜索URL:shopsURL){
HttpClient shopsClient=新的默认HttpClient();
试一试{
//传递要获取的搜索URL字符串
HttpGet shopsGet=新的HttpGet(搜索URL);
//执行请求
HttpResponse shopsResponse=shopsClient.execute(shopsGet);
//检查状态,仅在确定时继续
StatusLine searchStatus=shopsResponse.getStatusLine();
if(searchStatus.getStatusCode()==200){
//得到回应
HttpEntity shopsEntity=shopsResponse.getEntity();
InputStream shopsContent=shopsEntity.getContent();
//处理结果
InputStreamReader shopsInput=新的InputStreamReader(shopsContent);
BufferedReader shopsReader=新的BufferedReader(shopsInput);
弦线;
而((lineIn=shopsReader.readLine())!=null){
shopsFeedBuilder.append(lineIn);
}
}
其他的
shopsDisplay.setText(“哎呀,出了点问题!”);
}
捕获(例外e){
shopsDisplay.setText(“哎呀,出了点问题!”);
e、 printStackTrace();
}
}
//返回结果字符串
return shopsFeedBuilder.toString();
}
/*
*处理搜索查询的结果
*-接收包含搜索词的代表商店的JSON字符串
*/
受保护的void onPostExecute(字符串结果){
//开始准备要显示的结果字符串
StringBuilder shopsResultBuilder=新StringBuilder();
试一试{
//从结果中获取JSONObject
JSONObject resultObject=新的JSONObject(结果);
//获取检索到的JSONObject中包含的JSONArray-“结果”
JSONArray shopsArray=resultObject.getJSONArray(“shops”);
//循环浏览shops数组中的每个项目
对于(int t=0;t0)
shopsDisplay.setText(shopsResultBuilder.toString());
其他的
shopsDisplay.setText(“对不起,没有找到可供您搜索的店铺!”);
}
}
}
对于JSONObject
,使用标准Java文件I/O将文件读入字符串
,然后将其传递给JSONObject
构造函数
但是,如果您有一个大的JSON文件,您可能希望切换到其他JSON解析器(例如),这可能会给您提供不同的选项(例如,使用FileReader
和JsonReader
)
public class Main extends Activity {
private TextView shopsDisplay;
private static String searchURL = "http://example.com/sample.json";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.baby);
//reference throughout class
shopsDisplay = (TextView)findViewById(R.id.tweet_txt);
new GetShops().execute(searchURL);
}
private class GetShops extends AsyncTask<String, Void, String> {
/*
* Carry out fetching task in background
* - receives search URL via execute method
*/
@Override
protected String doInBackground(String... shopsURL) {
//start building result which will be json string
StringBuilder shopsFeedBuilder = new StringBuilder();
//should only be one URL, receives array
for (String searchURL : shopsURL) {
HttpClient shopsClient = new DefaultHttpClient();
try {
//pass search URL string to fetch
HttpGet shopsGet = new HttpGet(searchURL);
//execute request
HttpResponse shopsResponse = shopsClient.execute(shopsGet);
//check status, only proceed if ok
StatusLine searchStatus = shopsResponse.getStatusLine();
if (searchStatus.getStatusCode() == 200) {
//get the response
HttpEntity shopsEntity = shopsResponse.getEntity();
InputStream shopsContent = shopsEntity.getContent();
//process the results
InputStreamReader shopsInput = new InputStreamReader(shopsContent);
BufferedReader shopsReader = new BufferedReader(shopsInput);
String lineIn;
while ((lineIn = shopsReader.readLine()) != null) {
shopsFeedBuilder.append(lineIn);
}
}
else
shopsDisplay.setText("Whoops - something went wrong!");
}
catch(Exception e){
shopsDisplay.setText("Whoops - something went wrong!");
e.printStackTrace();
}
}
//return result string
return shopsFeedBuilder.toString();
}
/*
* Process result of search query
* - this receives JSON string representing shops with search term included
*/
protected void onPostExecute(String result) {
//start preparing result string for display
StringBuilder shopsResultBuilder = new StringBuilder();
try {
//get JSONObject from result
JSONObject resultObject = new JSONObject(result);
//get JSONArray contained within the JSONObject retrieved - "results"
JSONArray shopsArray = resultObject.getJSONArray("shops");
//loop through each item in the shops array
for (int t=0; t<shopsArray.length(); t++) {
//each item is a JSONObject
JSONObject shopsObject = shopsArray.getJSONObject(t);
//for if condition
String id = (String) shopsObject.get("id");
//get the name and description for each shops
if (id.equals("550")){
shopsResultBuilder.append(shopsObject.getString("name")+": ");
shopsResultBuilder.append(shopsObject.get("description")+"\n\n");
}
}
}
catch (Exception e) {
shopsDisplay.setText("Whoops - something went wrong!");
e.printStackTrace();
}
//check result exists
if(shopsResultBuilder.length()>0)
shopsDisplay.setText(shopsResultBuilder.toString());
else
shopsDisplay.setText("Sorry - no shops found for your search!");
}
}
}