2016-07-29 7 views
0

Ich brauche eine String-Variable, die ich von einer Server-Antwort bekomme. Ich habe versucht, mit Shared Einstellungen zu speichern, oder nur um mich abzumelden, aber ich kann nicht.Kann Antwort vom Server mit Sharedpreferences nicht speichern

Es ist in der gleichen Aktivität, aber ich habe versucht, es auch auf eine andere Aktivität zu zeigen.

Das Problem liegt im Speichervorgang.

String content_url=""; 

public void onResponse(JSONObject response) { 

    try { 

      Log.i("dataJson", response.toString()); 
       for (int i = 0; i < response.length(); i++) { 
       JSONObject jresponse = response.getJSONObject("dataJson"); 
         String media_id = jresponse.getString("media_id"); 
         String session_id = jresponse.getString("session_id"); 
         String content_url = jresponse.getString("content_url"); 

         Log.i("dataJson", media_id); 
         Log.i("dataJson", session_id); 
         Log.i("dataJson", content_url); 

         } 

SharedPreferences sharedPref = getSharedPreferences("server", Context.MODE_PRIVATE); 

SharedPreferences.Editor editor = sharedPref.edit(); 
editor.putString("content_url", content_url); 
editor.apply(); 


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

Und zum Abrufen:

SharedPreferences sharedPref =getSharedPreferences("server", Context.MODE_PRIVATE); 

    String content_url1 =sharedPref.getString("content_url", ""); 
    Log.i("dataJson", "Content: " + content_url1); 

das Protokoll Ergebnis: 07-29 14: 55: 30,362 11.522-11.522 /? I/dataJson: Inhalt:

Ich habe versucht, woanders sharedpref Teil, aber das Ergebnis ist das gleiche .. = nichts.

was mache ich falsch?

die complett Antwort vom Server:

07-29 15:21:40.442 13585-13585/? I/dataJson: {"media_id":2104,"session_id":"14697845603416","content_url":"http:\/\/developlment.dev.tech.com\/upload\/2104\/content"} 
+0

editor.commit() .. fehlende remove editor.apply(); –

+0

Warum verwenden Sie eine 'for' -Schleife für ein' JSONObject'? –

+1

commit und apply sind gleich .. aber ich änderte, nichts Neues – Janos

Antwort

0

Gebrauchte

editor.commit(); 

Statt

editor.apply(); 

Hinweis: Setzen Sie in Ihrem Sharedpreference for-Schleife

for (int i = 0; i < response.length(); i++) { 
      JSONObject jresponse = response.getJSONObject("dataJson"); 
        String media_id = jresponse.getString("media_id"); 
        String session_id = jresponse.getString("session_id"); 
        String content_url = jresponse.getString("content_url"); 

        Log.i("dataJson", media_id); 
        Log.i("dataJson", session_id); 
        Log.i("dataJson", content_url); 


      SharedPreferences sharedPref = getSharedPreferences("server", Context.MODE_PRIVATE); 

      SharedPreferences.Editor editor = sharedPref.edit(); 
      editor.putString("content_url", content_url); 
      editor.commit(); 

        } 
+0

so versucht, das Ergebnis ist das gleiche. – Janos

+0

Post your Json Ergebnis .. –

+0

07-29 15: 11: 20.472 12999-12999 /? Ich/dataJson: {"media_id": 2094, "session_id ":" 14697845603416 "," content_url ":" http: \/\/develop.dev.tech.com \/upload \/2094 \/content "} – Janos

0

ändern String content_url = jresponse.getString("content_url");-content_url = jresponse.getString("content_url");

Sie die gleiche Variable in der for Schleife und so Ihre erste Variable erneut erklärt ist immer noch leer. Sie müssen die Anfangsvariable mit den Daten aktualisieren, die Sie erhalten.

Ändern Sie auch editor.apply(); in editor.commit(); und protokollieren Sie das Ergebnis von content_url, bevor Sie in SharedPreferences speichern.

Edit:

Ersetzen Sie Code mit diesem. Sie müssen nur die Daten aus der Antwort abrufen. Es ist keine for Schleife erforderlich, da Sie die Schlüssel im JSONObject bereits kennen.

public void onResponse(JSONObject response) { 
    try { 
     Log.i("dataJson", response.toString()); 
     String media_id = response.getString("media_id"); 
     String session_id = response.getString("session_id"); 
     String content_url = response.getString("content_url"); 

     Log.i("dataJson", media_id); 
     Log.i("dataJson", session_id); 
     Log.i("dataJson", content_url); 

     SharedPreferences sharedPref = getSharedPreferences("server", Context.MODE_PRIVATE); 
     SharedPreferences.Editor editor = sharedPref.edit(); 
     editor.putString("content_url", content_url).commit(); 

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

das Ergebnis ist gleich .. nichts – Janos

+0

Siehe die bearbeitete Antwort. –

+0

Ich korrigierte wie du sagst, ich habe nichts – Janos