Android 在设备上保存从网站parse.com接收的数据

Android 在设备上保存从网站parse.com接收的数据,android,parse-platform,Android,Parse Platform,如何在android设备中保存来自parse.com的数据 我正在尝试将数据保存在应用程序中的某个位置,以便在没有网络的情况下,客户仍然可以将数据保存在设备上 我正在使用此代码创建列表,并且正在工作,只是缺少了在没有internet时保存数据的问题 package com.example.ronysueliton.patospizzas; import android.app.ActionBar; import android.content.Intent; import android.gr

如何在android设备中保存来自parse.com的数据

我正在尝试将数据保存在应用程序中的某个位置,以便在没有网络的情况下,客户仍然可以将数据保存在设备上

我正在使用此代码创建列表,并且正在工作,只是缺少了在没有internet时保存数据的问题

package com.example.ronysueliton.patospizzas;

import android.app.ActionBar;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;

/**
 * Created by Rony Sueliton on 11/04/2015.
 */
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.parse.FindCallback;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

public class Pizzarias extends ActionBarActivity {

 // Declare Variables
 Boolean bColorStatus = true;
 TextView status;
 ListView listview;
 List<ParseObject> ob;

 ProgressDialog mProgressDialog;
 ListViewAdapterPizzarias adapter;
 private List<WorldPopulation> worldpopulationlist = null;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_pizzarias);

 new RemoteDataTask().execute();

 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 // Inflate the menu; this adds items to the action bar if it is present.
 getMenuInflater().inflate(R.menu.menu_pizzarias, menu);

 //Os metodos abaixo são para mostrar o icone do aplicativo na action bar
 getSupportActionBar().setDisplayShowHomeEnabled(true);
 getSupportActionBar().setLogo(R.drawable.ic_launcher);
 getSupportActionBar().setDisplayUseLogoEnabled(true);

 return true;
 }


 /*@Override
 public boolean onOptionsItemSelected(MenuItem item) {
 onBackPressed();
 return true;
 }*/

 //RemoteDataTask AsyncTask
 private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
 @Override
 protected void onPreExecute() {
 super.onPreExecute();
 // Create a progressdialog
 mProgressDialog = new ProgressDialog(Pizzarias.this);
 // Set progressdialog title
 mProgressDialog.setTitle("Carregando Pizzarias");
 // Set progressdialog message
 mProgressDialog.setMessage("Aguarde...");
 mProgressDialog.setIndeterminate(false);
 // Show progressdialog
 mProgressDialog.show();
 }

 @Override
 protected Void doInBackground(Void... params) {
 // Create the array
 worldpopulationlist = new ArrayList<WorldPopulation>();

 try {
 // Locate the class table named "Country" in Parse.com
 ParseQuery query = new ParseQuery("GerenciarPizzariasPatos");

 // Locate the column named "ranknum" in Parse.com and order list
 // by ascending,

 query.orderByAscending("nome");

 ob = query.find();
 for (ParseObject country : ob) {
 WorldPopulation map = new WorldPopulation();
 map.setNome((String) country.get("nome"));
 map.setEndereco((String) country.get("endereco"));
 map.setTelefone((String) country.get("telefone"));
 map.setStatus((String) country.get("status"));
 worldpopulationlist.add(map);
 }
 } catch (ParseException e) {
 Log.e("Error", e.getMessage());
 e.printStackTrace();
 }
 return null;
 }

 @Override
 protected void onPostExecute(Void result) {
 // Locate the listview in listview_main.xml
 listview = (ListView) findViewById(R.id.listviewpizzarias);
 // Pass the results into ListViewAdapter.java
 adapter = new ListViewAdapterPizzarias(Pizzarias.this,
 worldpopulationlist);
 // Binds the Adapter to the ListView
 listview.setAdapter(adapter);
 // Close the progressdialog
 mProgressDialog.dismiss();
 }
 }
}

只需添加
pin(map)
世界人口列表之后添加(地图)并添加到逻辑,该逻辑将检查连接是否可用,并将启用
query.fromLocalDatastore()
选项如果在执行查询之前连接不可用

我将我的代码行更改为此,我尝试使用internet parse.com博客的教程,但给出了一个错误GET,用红线下划线

我尝试使用该站点的一个示例:

@覆盖
受保护的Void doInBackground(Void…参数){
//创建数组
worldpopulationlist=新建ArrayList();
试一试{
//在Parse.com中找到名为“Country”的类表
//ParseQuery查询=新的ParseQuery(“GerenciarPizzariasPatos”);
字符串objectId=“GFiBnjCE4v”;
ParseObject提要=ParseQuery.get(objectId);//错误获取//联机ParseQuery结果
feed.fetch();
feed.put(“fechado”,true);
feed.savefinally();
//ParseQuery=ParseQuery.getQuery(“GerenciarPizzariasPatos”);
ParseQuery query=ParseQuery.get(“gerenciarpizzariapatos”)//获取错误
.fromLocalDatastore()
.whereEquals(“fechado”,真)
.findInBackground(新的FindCallback(){
公共void done(列出对象,parsee异常){
//“提要”ParseObject将在结果列表中返回
}
});
//在Parse.com和订单列表中找到名为“ranknum”的列
//通过提升,
query.orderByAscending(“nome”);
ob=query.find();
用于(对象国家:ob){
世界人口地图=新世界人口();
map.setNome((字符串)country.get(“nome”);
map.setEndereco((字符串)country.get(“endereco”);
map.setTelefone((String)country.get(“telefone”);
map.setStatus((字符串)country.get(“status”);
worldpopulationlist.add(地图);
}
}捕获(解析异常){
Log.e(“Error”,e.getMessage());
e、 printStackTrace();
}
返回null;
}

@Vivart。我做过但没有成功,我想知道如何在我的代码中正确实现它,因为我尝试了很多方法,但没有管理。可能你的应用程序中有一个数据库,可以保存数据,并在互联网关闭时为你服务。只需刷新数据库中的数据一段时间,即可获得更新的数据。@RonySueliton您是否使用Parse.enableLocalDatastore(getApplicationContext())启用了本地数据库;在应用课上?@Vivart。这是
(getApplicationContext())我没有,只是把
Parse.enableLocalDatastore(这个)他在教程中讲话的地方,该位置必须放置,但已放置且未起作用。@Oleg Osipenko。我在代码中做了一个更改,试图使用教程,但在get中收到一个错误,该错误用红线下划线,编译时出现此错误:
error:(100,46)error:non-static method get(String)无法从静态上下文中引用,其中T是类型变量:T扩展ParseQuery类中声明的ParseObject
@async。现在看看我要实现什么。@Yarh。你可以看看我的问题吗?
public class ParseApplication extends Application {


    @Override
    public void onCreate() {
        super.onCreate();

        Parse.enableLocalDatastore(this);

        // Add your initialization code here
        Parse.initialize(this, "xxx", "xxx");

        ParseUser.enableAutomaticUser();
        ParseACL defaultACL = new ParseACL();

        // If you would like all objects to be private by default, remove this
        // line.
        defaultACL.setPublicReadAccess(true);

        ParseACL.setDefaultACL(defaultACL, true);
    }

}
@Override
    protected Void doInBackground(Void... params) {
        // Create the array
        worldpopulationlist = new ArrayList<WorldPopulation>();


        try {
            // Locate the class table named "Country" in Parse.com
            //ParseQuery query = new ParseQuery("GerenciarPizzariasPatos");
            String objectId = "GFiBnjCE4v";
            ParseObject feed = ParseQuery.get(objectId); // error GET // Online ParseQuery result
            feed.fetch();
            feed.put("fechado", true);
            feed.saveEventually();

            //ParseQuery<ParseObject> query = ParseQuery.getQuery("GerenciarPizzariasPatos");
            ParseQuery<ParseObject> query = ParseQuery.get("GerenciarPizzariasPatos") // error GET 
                    .fromLocalDatastore()
                    .whereEquals("fechado", true)
                    .findInBackground(new FindCallback<ParseObject>() {
                        public void done(List<ParseObject> objects, ParseException e) {
                            // "feed" ParseObject will be returned in the list of results
                        }
                    });

            // Locate the column named "ranknum" in Parse.com and order list
            // by ascending,

            query.orderByAscending("nome");

            ob = query.find();
            for (ParseObject country : ob) {
                WorldPopulation map = new WorldPopulation();
                map.setNome((String) country.get("nome"));
                map.setEndereco((String) country.get("endereco"));
                map.setTelefone((String) country.get("telefone"));
                map.setStatus((String) country.get("status"));
                worldpopulationlist.add(map);

            }
        } catch (ParseException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }