編輯:關於android開發
可以使用網絡來保存數據,在需要的時候從網絡上獲取數據,進而顯示在App中。
用網絡保存數據的方法有很多種,對於不同的網絡數據采用不同的上傳與獲取方法。
本文利用LeanCloud來進行網絡數據的存儲。
LeanCloud是一種簡單高效的數據和文件存儲服務。感興趣的可以查看網址:https://leancloud.cn/。關於LeanCloud的數據存儲使用方法可以在裡面找到,本文不講述關於LeanCloud的使用,知識借助LeanCloud平台舉一個在網絡上存儲數據的例子。
AVObject personObject = new AVObject(TABLENAME); personObject.put(NAME, person.name); personObject.put(AGE, person.age); personObject.put(INFO, person.info); personObject.saveInBackground(new SaveCallback() { @Override public void done(AVException e) { if (e == null) { Log.v(TAG, "put data success!"); } else { Log.v(TAG, "put data failed!error:" + e.getMessage()); } } });
AVQuery<AVObject> avQuery = new AVQuery<>(TABLENAME); avQuery.findInBackground(new FindCallback<AVObject>() { @Override public void done(List<AVObject> list, AVException e) { if (e == null) { Log.v(TAG, "get data success!"); String message = ""; for (int i = 0; i < list.size(); i++) { String name = list.get(i).getString(NAME); int age = list.get(i).getInt(AGE); String info = list.get(i).getString(INFO); message += "name:" + name + ",age:" + age + ",info:" + info + ".\n"; } textView.setText(message); } } });
<string name="network">Network</string> <string name="get_data">獲取數據</string> <string name="put_data">上傳數據</string>
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context="com.zhangmiao.datastoragedemo.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="@string/network" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="@dimen/fab_margin" android:layout_marginTop="@dimen/fab_margin" android:orientation="horizontal"> <Button android:id="@+id/network_put" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/put_data" /> <Button android:id="@+id/network_get" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/get_data" /> </LinearLayout> <TextView android:id="@+id/table_info" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/app_name" /> </LinearLayout> </android.support.design.widget.CoordinatorLayout>
package com.zhangmiao.datastoragedemo; import android.util.Log; import android.widget.TextView; import com.avos.avoscloud.AVException; import com.avos.avoscloud.AVObject; import com.avos.avoscloud.AVQuery; import com.avos.avoscloud.FindCallback; import com.avos.avoscloud.SaveCallback; import java.util.List; /** * Created by zhangmiao on 2016/12/22. */ public class NetworkDBManager { private static final String TAG = "NetworkDBManager"; private final static String TABLENAME = "person"; private final static String NAME = "name"; private final static String AGE = "age"; private final static String INFO = "info"; public void putData(Person person) { AVObject personObject = new AVObject(TABLENAME); personObject.put(NAME, person.name); personObject.put(AGE, person.age); personObject.put(INFO, person.info); personObject.saveInBackground(new SaveCallback() { @Override public void done(AVException e) { if (e == null) { Log.v(TAG, "put data success!"); } else { Log.v(TAG, "put data failed!error:" + e.getMessage()); } } }); } public void getData(final TextView textView) { AVQuery<AVObject> avQuery = new AVQuery<>(TABLENAME); avQuery.findInBackground(new FindCallback<AVObject>() { @Override public void done(List<AVObject> list, AVException e) { if (e == null) { Log.v(TAG, "get data success!"); String message = ""; for (int i = 0; i < list.size(); i++) { String name = list.get(i).getString(NAME); int age = list.get(i).getInt(AGE); String info = list.get(i).getString(INFO); message += "name:" + name + ",age:" + age + ",info:" + info + ".\n"; } textView.setText(message); } } }); } }
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
package com.zhangmiao.datastoragedemo; import android.content.ContentResolver; import android.content.ContentValues; import android.database.Cursor; import android.net.*; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.avos.avoscloud.AVOSCloud; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener {private NetworkDBManager mNetworkDBManager; private TextView mTableInfo; @Override protected void onCreate(Bundle savedInstanceState) { Log.v("MainActivity", "onCreate"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AVOSCloud.initialize(this, "yMNUazdBt872mNtC9aSakjYy-gzGzoHsz", "d4vw3VYdMCjLpsXRhHTBRutC");
mNetworkDBManager = new NetworkDBManager(); Button networkGet = (Button) findViewById(R.id.network_get); Button networkPut = (Button) findViewById(R.id.network_put); mTableInfo = (TextView) findViewById(R.id.table_info); networkGet.setOnClickListener(this); networkPut.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) {case R.id.network_put: Person person3 = new Person("xiao", 23, "women"); Person person4 = new Person("zhao", 24, "men"); mNetworkDBManager.putData(person3); mNetworkDBManager.putData(person4); break; case R.id.network_get: mNetworkDBManager.getData(mTableInfo); break; default: Log.v("MainActivity", "default"); break; } } }
代碼下載地址:https://github.com/ZhangMiao147/DataStorageDemo
RecyclerView的五大開源項目-解決辦法 前段時間做項目由於采用的MD設計,所以必須要使用RecyclerView全面代替ListView。但是開發中遇到了
Android Studio多渠道打包 我們開發一個APP在上傳應用市場之前,有時候會遇到要根據不同平台打多個apk包的問題。由於Android的應用市場比較多,主流的應
Android 獲取當前網速質量調整網絡請求 在開發中,有時候常常需要根據用戶當前的網速來做一些操作,比如圖片的加載,當網速非常好的時候,比如連接的是wifi,我們就
Android開發之Menu:OptionMenu(選項菜單)、ContextMenu(上下文菜單)、SubMenu(子菜單) 菜單的概念,現在已經很普及了。 W