1

嗨,我根据本教程从 http 服务器创建了解析 JSON 。但我想从 sdcard 解析这个 JSON 文件。我可以使用 Environment.getExternalStorageDirectory().getAbsolutePath() 打印 json 文件位置,但我不知道如何更改 AsyncTask 读取文件。有人可以帮我做这件事吗?(我是 android 开发新手)代码如下所示:

public class ClientActivity extends Activity {

    TextView capitalTextView;
    ProgressDialog progressDialog;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.main1);
        capitalTextView = (TextView) findViewById(R.id.capital_textview);

        this.retrieveCapitals();
    }

    void retrieveCapitals() {

        progressDialog = ProgressDialog.show(this,
                "Please wait...", "Retrieving data...", true, true);

        CapitalsRetrieverAsyncTask task = new CapitalsRetrieverAsyncTask();
        task.execute();
        progressDialog.setOnCancelListener(new CancelListener(task));       
    }

    private class CapitalsRetrieverAsyncTask extends AsyncTask<Void, Void, Void> {

        Response response;

        @Override
        protected Void doInBackground(Void... params) {
            String url = "http://sample.com/sample_data.json";
            HttpGet getRequest = new HttpGet(url);

            File file = new File( Environment.getExternalStorageDirectory().getAbsolutePath() + "/example.json");
            System.out.println(file);

            try {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpResponse getResponse = httpClient.execute(getRequest);
                final int statusCode = getResponse.getStatusLine().getStatusCode();

                if (statusCode != HttpStatus.SC_OK) { 
                    Log.w(getClass().getSimpleName(), "Error " + statusCode + " for URL " + url); 
                    return null;
                }

                HttpEntity getResponseEntity = getResponse.getEntity();
                InputStream httpResponseStream = getResponseEntity.getContent();
                Reader inputStreamReader = new InputStreamReader(httpResponseStream);

                Gson gson = new Gson();
                this.response = gson.fromJson(inputStreamReader, Response.class);
                System.out.println(this.response);
            } 
            catch (IOException e) {
                getRequest.abort();
                Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            StringBuilder builder = new StringBuilder();
            for (Shop shop : this.response.shops) {

                builder.append(String.format("<br>ID: <b>%s</b><br>Shop: <b>%s</b><br>Description: <b>%s</b><br><br>", shop.getId(), shop.getName(), shop.getDescription()));

            }

            capitalTextView.setText(Html.fromHtml(builder.toString()));
            progressDialog.cancel();
        }

    }

    private class CancelListener implements OnCancelListener {

        AsyncTask<?, ?, ?> cancellableTask;

        public CancelListener(AsyncTask<?, ?, ?> task) {
            cancellableTask = task;
        }

        @Override
        public void onCancel(DialogInterface dialog) {
            cancellableTask.cancel(true);
        }

    }

}
4

1 回答 1

0

不要按原样保存 JSON 文件。JSON 用于将值从一个地方传输到另一个地方。

相反,根据数据,您可以使用 Shared Preference 或 SQLite 数据库来存储它。看看这个:http: //developer.android.com/guide/topics/data/data-storage.html

然后您可以轻松地检索它并对数据进行修改。

于 2013-08-25T22:49:19.747 回答