我正在尝试执行多个请求,并且我将共享首选项中的数据保存为第二个共享首选项代码:
public void makeMultipleRequests()
{
for(i=0;i<districtid.length;i++)
{
districtId=districtid[i];
String url="http://49.50.72.188/epdswebsite/webservice.asmx/GetDFSCDetails?DistrictId="+districtId;
dfsc = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// TODO Auto-generated method stub
dialog.dismiss();
try {
if (dialog.isShowing()){
dialog.dismiss();
}
JSONArray ar = response.getJSONArray("DFSCDetails");
JSONObject data= ar.getJSONObject(0);
String ema=data.getString("Email");
String mob=data.getString("Mobile");
String off= data.getString("LandLine");
String ser= data.getString("DFSCName");
String tit = data.getString("District_Name");
String add=data.getString("Address");
String disid= data.getString("District_Number");
preference = getSharedPreferences("Dfsc"+disid, 0);
SharedPreferences.Editor edit = preference.edit();
edit.putBoolean("dfscboolean", true);
edit.putString("serial", ser);
edit.putString("office", off);
edit.putString("title", tit);
edit.putString("address", add);
edit.putString("mobile", mob);
edit.putString("email", ema);
edit.apply();
dialog.dismiss();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
dialog.dismiss();
}}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
String surl="http://49.50.72.188/epdswebsite/webservice.asmx/GetFSODetailByDistrict?DistrictId="+districtId;
JsonObjectRequest fso= new JsonObjectRequest(Request.Method.GET, surl, null, new Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// TODO Auto-generated method stub
try {
JSONArray ar = response.getJSONArray("FSOModelDetails");
JSONObject data= ar.getJSONObject(0);
String add= data.getString("Office_Address");
String mob= data.getString("Personal_Mobile");
String nam = data.getString("Name");
String dis = data.getString("District_Name");
SharedPreferences mPrefs = getSharedPreferences("Fso"+districtId, 0);
SharedPreferences.Editor edit = mPrefs.edit();
edit.putBoolean("fsoboolean", true);
edit.putString("address", add);
edit.putString("mobile",mob);
edit.putString("name", nam);
edit.putString("district", dis);
edit.apply();
System.out.println(mPrefs.getString("address", "failed"));
dialog.dismiss();
dialog.dismiss();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
dialog.dismiss();
Toast.makeText(getApplicationContext(), "Failed! Could not connect to the server!", Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
// TODO Auto-generated method stub
dialog.dismiss();
Toast.makeText(getApplicationContext(), "Failed! Could not connect to the server!", Toast.LENGTH_SHORT).show();
}
});
request.add(dfsc);
request.add(fso);
}
`*
这是我从enter code here
共享偏好设置中获取数据的第二个活动:
public void loadstaticdata()
{
SharedPreferences mPrefs = getSharedPreferences("Fso"+districtId, 0);
String addres=mPrefs.getString("address", "");
String nam=mPrefs.getString("name", "");
String distric=mPrefs.getString("district", "");
String mobil=mPrefs.getString("mobile", "");
System.out.println(addres);
System.out.println(distric);
System.out.println(mobil);
System.out.println(nam);
address.setText(addres);
name.setText(nam);
district.setText(distric);
mobile.setText(mobil);
}
我哪里错了?
答案 0 :(得分:0)
首先,如果您要保存大量数据,则应选择SQLite
之类的方法。
无论如何,既然你在项目中使用了这个,我想直接回答你的问题。
而不是edit.apply();
尝试使用edit.commit();
。
commit()
方法在主线程上运行并将阻塞。 apply()
方法以异步方式运行。
答案 1 :(得分:0)
确保SharedPreferences对PreferenceManager.getDefaultSharedPreferences
提交任何数据:
String myValue="My Value";
SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor edit = preference.edit();
edit.putString("KEY", myValue);
/* Your Code */
edit.commit();
要检索它:
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
String myValue=mPrefs.getString("KEY", "");
/* Your code */