【问题标题】:store web service data to shared preference将 Web 服务数据存储到共享首选项
【发布时间】:2018-06-10 09:48:14
【问题描述】:

。为什么每次投票都是负面的..我是android的新手;(。你的解决方案可能会帮助其他人...... 我有一个应用程序。在登录时,我想将数据存储到共享首选项。

我正在尝试将数据从服务器保存到Shared Preferences。我正在成功地从服务器接收数据

我试过了,但是有一些错误

{"Login":"Success","Login Details":[{"name":"abc","place":"abcdd","cityname":"asdf"}]}

代码是:

  public class login extends AsyncTask<Void, Void, Void> {

    InputStream ins;
    String status,details, result, s = null, data = "", js;
    int ss;
    int responseCode;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected Void doInBackground(Void... params) {
        StringBuilder sb = new StringBuilder();
        ArrayList al;
        try {
            URL url =new URL(BuildConfig.url);

            String param = "username=" + uname + "&password=" + pass2;
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setConnectTimeout(15000);
            connection.setReadTimeout(15000);
            connection.setDoInput(true);
            connection.setDoOutput(true);

            OutputStream os = connection.getOutputStream();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            bw.write(param);
            bw.flush();
            bw.close();

            responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line = "";
                while ((line = br.readLine()) != null) {
                    sb.append(line + "\n");
                }
            }
            data = sb.toString();
            JSONObject json = new JSONObject(data);

            status = json.getString("Login");


        } catch (MalformedURLException e) {
            Log.i("MalformedURLException", e.getMessage());
        } catch (IOException e) {
            Log.i("IOException", e.getMessage());
        } catch (JSONException e) {
            Log.i("JSONException", e.getMessage());
        }

        return null;
    }

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

            String status1 = status.trim();


            if (status1.equals("Success")) {

                try {
                    JSONObject  jsonObject = new JSONObject(status1);
                    JSONArray array=jsonObject.getJSONArray("Login Details");

                    for (int i=0;i<array.length();i++){


                        jsonObject = array.getJSONObject(i);

                        name= jsonObject.getString("name");
                        place= jsonObject.getString("place");
                        cityname = jsonObject.getString("cityname");


                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                SharedPreferences.Editor editor = sharedPreferences.edit();

                editor.putString("name", name);
                editor.putString("place", place);
                editor.apply();
                Toast.makeText(Login.this,name+":"+cityname+":"+mobileno,Toast.LENGTH_SHORT).show();

            }
            else {
                 Toast.makeText(Login.this, "Username or Password is Incorrect", Toast.LENGTH_LONG).show();
                }



        }

    }

【问题讨论】:

  • 哪里有错误?
  • 我的代码是对的??
  • 如果你得到了预期的输出,那么它是正确的。如果有任何错误,请解释
  • 我没有得到结果,当我吐司 3 个值 o/p 为 null:null:null
  • 我在这里更改实际值,忘记更改 toast 值

标签: android json web-services sharedpreferences


【解决方案1】:

您的“JSONArray”仅包含一个 JSONObject 和您的 using 循环

try {
                    JSONObject  jsonObject = new JSONObject(status1);
                    JSONArray array=jsonObject.getJSONArray("Login Details");
//"Login Details":[{"name":"abc","place":"abcdd","cityname":"asdf"}] only one object 
                    for (int i=0;i<array.length();i++){


                        jsonObject = array.getJSONObject(i);

                        name= jsonObject.getString("name");
                        place= jsonObject.getString("place");
                        cityname = jsonObject.getString("cityname");


                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

我希望你能理解并且这个提示对你有用 并且请在解析和打印时使用相同的变量

【讨论】:

  • @Avinash logindetail jsonArray 只有一个 jsonobject,它自己有 3 个术语 name , place , cityname 如果您在阅读 jsonArray 和 JsonObject 时有任何困惑,请了解这些术语实际上是什么
【解决方案2】:

首先status不是一个json对象,它是一个字符串,确保你的doinbackground返回正确的结果,然后使用JSONObject jsonObject = new JSONObject(result);而不是JSONObject jsonObject = new JSONObject(status1);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    • 2011-09-03
    相关资源
    最近更新 更多