Android 将web服务数据存储到共享首选项

Android 将web服务数据存储到共享首选项,android,json,web-services,sharedpreferences,Android,Json,Web Services,Sharedpreferences,。为什么每次投票都是负面的。。我是android新手;)你的建议可能有助于其他人。。 我有一个应用程序。在登录时,我想将数据存储到共享首选项 我正在尝试将数据从服务器保存到共享首选项中。我正在成功地从服务器接收数据 我试过了,但有一些错误 {"Login":"Success","Login Details":[{"name":"abc","place":"abcdd","cityname":"asdf"}]} 代码为: public class login extends AsyncTas

。为什么每次投票都是负面的。。我是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();
                }



        }

    }
公共类登录扩展异步任务{
输入流;
字符串状态、详细信息、结果,s=null,data=“”,js;
int-ss;
国际响应码;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的Void doInBackground(Void…参数){
StringBuilder sb=新的StringBuilder();
ArrayList al;
试一试{
URL=新URL(BuildConfig.URL);
字符串param=“username=“+uname+”&password=“+pass2;
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod(“POST”);
连接。设置连接超时(15000);
连接。setReadTimeout(15000);
connection.setDoInput(true);
connection.setDoOutput(真);
OutputStream os=connection.getOutputStream();
BufferedWriter bw=新的BufferedWriter(新的OutputStreamWriter(os,“UTF-8”));
写入(参数);
bw.flush();
bw.close();
responseCode=connection.getResponseCode();
if(responseCode==HttpURLConnection.HTTP\u确定){
BufferedReader br=新的BufferedReader(新的InputStreamReader(connection.getInputStream());
字符串行=”;
而((line=br.readLine())!=null){
sb.追加(第+行“\n”);
}
}
data=sb.toString();
JSONObject json=新的JSONObject(数据);
status=json.getString(“登录”);
}捕获(格式错误){
i(“MalformedURLException”,e.getMessage());
}捕获(IOE异常){
Log.i(“IOException”,即getMessage());
}捕获(JSONException e){
i(“JSONException”,e.getMessage());
}
返回null;
}
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
字符串status1=status.trim();
如果(状态1.equals(“成功”)){
试一试{
JSONObject JSONObject=新JSONObject(状态1);
JSONArray数组=jsonObject.getJSONArray(“登录详细信息”);

对于(inti=0;i您的“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();
                }
试试看{
JSONObject JSONObject=新JSONObject(状态1);
JSONArray数组=jsonObject.getJSONArray(“登录详细信息”);
//“登录详细信息”:[{“名称”:“abc”,“地点”:“abcdd”,“城市名称”:“asdf”}]仅一个对象

对于(int i=0;i首先
status
不是json对象,而是字符串,请确保您的doinbackground返回正确的结果,然后使用
JSONObject JSONObject=new JSONObject(result);
而不是
JSONObject JSONObject=new JSONObject(status1)

哪里有错误?我的代码是对的??如果您得到了预期的输出,那么它是对的。如果有错误,那么解释我没有得到结果,当我toast 3个值时o/p为null:null:null我在这里更改实际值,忘记更改toastvalues@Avinashlogindetail jsonArray只有一个jsonobject,它本身有三个术语名称place、cityname如果您在阅读jsonArray和JsonObject时有任何困惑,请了解这些术语的实际含义