服务器中的数据未保存在“共享首选项”中

时间:2014-04-12 07:03:01

标签: android json sharedpreferences

我正在尝试将数据从服务器保存到共享首选项中。我正在从服务器成功接收数据。但我只能保存"名字"共享首选项中的字段。我不知道为什么其他字段没有像#34; First Name"。

那样保存

以下是Json的回复: -

"driver_details":[{"id":"8","first_name":"john","Surname":"williams","unit_no":"2","street_name":"paris","Suburb":"Melbourne","State":"Victoria","post_code":"0","driving_lic":"12345","authority_id":"12345","mobile_no":"6348998575","Email":"john@gmail.com","password":"827ccb0eea8a706c4c34a16891f84e7b","date_of_creation":"2014-04-03 07:26:10","status":"0","login_token":"3cdLDObQhtFFoOVUX9MH","latitude":"","longitude":""}],"err-code":"0"}

存储共享首选项的代码: -

                 pref=getSharedPreferences("Users", MODE_WORLD_READABLE);
                    SharedPreferences.Editor edit=pref.edit();

                    edit.putString("mfname", mfname);
                    edit.putString("msname", msname);
                    edit.putString("mun", mun);
                    edit.putString("mstreet", mstreet);
                    edit.putString("msuburb", msuburb);
                    edit.putString("mpassword", mpassword);
                    edit.putString("mpostcode", mpostcode);
                    edit.putString("mlicense", mlicense);
                    edit.putString("mid", mid);
                    edit.putString("mmobile", mmobile);
                    edit.putString("memail", memail);
                    edit.putString("login_token", login_token);
                    edit.commit();

用户(数据): -

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <null name="mmobile" />
    <null name="memail" />
    <null name="msname" />
    <string name="mfname">Abc</string>
    <null name="mstreet" />
    <null name="mpostcode" />
    <null name="mlicense" />
    <null name="mid" />
    <null name="login_token" />
    <null name="mpassword" />
    <null name="mun" />
    <null name="msuburb" />
</map>

解析代码: -

            StringEntity se = new StringEntity(job.toString());

            HttpPost httppost = new HttpPost("http://abc/webservice.php");

            httppost.setEntity(se);

            HttpResponse response = httpclient.execute(httppost);
              String tmpString = EntityUtils.toString(response.getEntity());

                String data = tmpString.replace("yes","");  
            System.out.println("response "+data);


            JSONObject jo = new JSONObject(data);

            Log.d("response", jo.toString(4));

            JSONArray jArray = jo.getJSONArray("driver_details");  
            Log.d("Array", jArray.toString(4));


            for (int i=0; i < jArray.length(); i++)
            {
                System.out.println("please print my name...for....");
                try {
                JSONObject jsob = jArray.getJSONObject(i);

                        mfname = jsob.getString("first_name");
                        msname = jsob.getString("surname");
                        mun = jsob.getString("unit_no");
                        mstreet = jsob.getString("street_name");
                        msuburb = jsob.getString("suburb");
                        mstate = jsob.getString("state");
                        mpassword = jsob.getString("password");
                        mpostcode = jsob.getString("post_code");
                        mlicense = jsob.getString("driving_license");
                        mid = job.getString("authority_id");
                        mmobile = jsob.getString("mobile_no");
                        memail = jsob.getString("email");
                        login_token=jsob.getString("login_token");

                 } catch (JSONException e) {

                } 

2 个答案:

答案 0 :(得分:2)

这一行是罪魁祸首

msname = jsob.getString("surname");

JSON的姓氏为资本S "Surname"而不是"surname"。如果您不知道它是否包含密钥,您可以尝试optString()。并且不要忘记打印exception

答案 1 :(得分:2)

memail,mmobile,msname等获取空值。

在存储共享偏好之前,请尝试打印日志中的所有值。