android - 将 Web 服务数据存储到共享首选项

标签 android json web-services sharedpreferences

。为什么每次投票都是负面的..我是android新手;(。你的建议可能会帮助其他人.. 我有一个申请。登录时,我想将数据存储到共享首选项。

我正在尝试将数据从服务器保存到共享首选项中。我已成功从服务器接收数据

我尝试过,但出现一些错误

{"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();
                }



        }

    }

最佳答案

您的“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();
                }

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

关于android - 将 Web 服务数据存储到共享首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48031311/

相关文章:

c# - 如何通过 PHP 调用 C# web 服务?

oracle - 通过 PL/SQL 访问 HTTPS Web 服务的问题

android - 多个自定义帐户与 GCMIntentService 结合使用

android - 在android中的数组列表中上下滚动时隐藏和显示标题

java - Gson将2个不同的类字段Mongo DB点映射到LatLng类

c# - 具有大结果的 Web 服务

java - Uri.parse(path) 在 Android 4.4.2 中返回 null

android - Java 中没有无符号类型,为什么 DEX 可执行文件中有无符号类型?

javascript - 如何访问 JSON 对象中的对象数组?

javascript - 如何从 JavaScript 输出 JSON 输出,以便将其识别为 JSON?