2017-10-13 3 views
0

In diesem Code werde ich senden ID und mobile_no es mich Kontostand des Benutzers abrufen, aber es konnte nicht in einem Toast drucken.in reprofit ich kann nicht drucken JSON

Was bin ich hier meine Arbeit richtig aber ich weiß nicht Antwortstring

private void Get_Wallet_Amount() { 
     Call<ResponseBody> call = AppController.getInstance().getApiInterface().getWalle_Data(Utils.getSharedPreference(getApplicationContext()).getString(Const.PREFERENCE_USER_ID,""), 
       Utils.getSharedPreference(getApplicationContext()).getString(Const.PREFERENCE_MOBILE_NUMBER,"")); 
     call.enqueue(new Callback<ResponseBody>() { 
      @Override 
      public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { 
       String responseString = null; 
       try { 
        Toast.makeText(Proceed_PaymentActivity.this, "HI", Toast.LENGTH_SHORT).show(); 
        responseString = response.body().string(); 
        JSONObject jsonObject = new JSONObject(responseString); 
//It doesn't print output 

       Toast.makeText(Proceed_PaymentActivity.this, ""+jsonObject.getString("wallet_amt"), Toast.LENGTH_SHORT).show(); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

     @Override 
     public void onFailure(Call<ResponseBody> call, Throwable t) { 
      Toast.makeText(Proceed_PaymentActivity.this, "Error", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

Retrofit-Schnittstelle

@FormUrlEncoded 
@POST("XXXX") 
Call<ResponseBody> getWalle_Data(@Field("id") String uid,@Field("mobile") String mobile); 

Meine JSON Ausgabe

erhalten
[ 
    { 
     "wallet_amt": 50 
    } 
] 

Fehler Logcat org.json.JSONException: Wert [{ "wallet_amt": 50}] vom Typ org.json.JSONArray kann nicht auf JSONObject 13.10 12.39 umgewandelt werden : 37.793 28522-28522/abc.example.com.reloaderappdesugn W/System.err: at org.json.JSON.typeMismatch (JSON.java:111) 10-13 12: 39: 37.793 28522-28522/abc.beispiel .com.Rechargeappdesugn W/System.err: at org.json.JSONObject. (JSONObject.java:160) 10-13 12: 39: 37.793 28522-28522/abc.example.com.reloaderappdesugn W/System.err: at org.json.JSONObject. (JSONObject.java:173) 10-13 12: 39: 37.793 28522-28522/abc.example.com.reloaderappdesugn W/System.err: bei abc.example.com.reloaderappdesugn.activity.Proceed_PaymentActivity $ 1.onResponse (Proceed_PaymentActivity.java:97) 10 -13 12: 39: 37.793 28522-28522/abc.example.com.reloaderappdesugn Mit System.err: bei retrofit2.ExecutorCallAdapterFactory $ ExecutorCallbackCall $ 1 $ 1.run (ExecutorCallAdapterFactory.java:68) 10-13 12: 39: 37.793 28522-28522/abc.example.com.reloaderappdesugn W/System.err: bei android.os.Handler.handleCallback (Handler.java:751) 10-13 12: 39: 37.793 28522-28522/abc.example.com .reloaderappdesugn W/System.err: at android.os.Handler.dispatchMessage (Handler.java:95) 10-13 12: 39: 37.793 28522-28522/abc.example.com.reloaderappdesugn W/System.err: at android.os.Looper.loop (Looper.java:154) 10-13 12: 39: 37.793 28522-28522/abc.example.com.reloaderappd esugn W/System.err: bei android.app.ActivityThread.main (ActivityThread.java:6119) 10-13 12: 39: 37.793 28522-28522/abc.example.com.reloaderappdesugn W/System.err: at java .lang.reflect.Method.invoke (systemeigene Methode) 10-13 12: 39: 37.793 28522-28522/abc.example.com.reloaderappdesugn W/System.err: at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller .run (ZygoteInit.java:886) 10-13 12: 39: 37.794 28522-28522/abc.example.com.reloaderappdesugn Mit System.err: at com.android.internal.os.ZygoteInit.main (ZygoteInit. java: 776)

+0

Es löst JsonParsingException. Überprüfen Sie Ihre Logcat – Kuls

+0

Gibt es irgendwelche Fehlermeldung/Ausnahmen ausgelöst? –

Antwort

2

Ihre json ist JSONArray.

Verwenden Sie JSONArray jsonArray = new JSONArray(responseString); in Ihrem Code.

müssen Sie getString() anstelle von opString() verwenden.

@Override 
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { 
    String responseString = null; 
    try { 
     Toast.makeText(Proceed_PaymentActivity.this, "HI", Toast.LENGTH_SHORT).show(); 
     responseString = response.body().string(); 
     // edited here 
     JSONArray jsonArray = new JSONArray(responseString); 
     for (int i = 0; i < jsonArray.length(); i++) { 
      JSONObject jsonObject = jsonArray.getJSONObject(i); 
      String wallet_amt = jsonObject.String("wallet_amt"); 
      Toast.makeText(Proceed_PaymentActivity.this, wallet_amt, Toast.LENGTH_SHORT).show(); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
+0

responseString = response.body(). String(); JSONArray jsonArray = neues JSONArray (responseString); JSON-Objekt jsonObject = jsonArray.getJSONObject (0); // hier habe ich einen Wert von 0 oder null es konnte nichts zeigen Utils.getSharedPreference (getApplicationContext()). Edit(). PutString (Const.PREFERENCE_Walllet_Amt, jsonObject.optString ("wallet_amt")); –

0

zunächst alles, was Sie brauchen nicht jede Datenmanipulation zu tun, um Antwort im Zusammenhang als Nachrüst-intern diese abwickelt. Bitte verwenden Sie das folgende Code-Snippet, damit es funktioniert.

class Wallet { 
     @SerializedName("wallet_amt") 
     private Long amount; 
    } 

Call<List<Wallet>> call = AppController.getInstance().getApiInterface().getWalle_Data(Utils.getSharedPreference(getApplicationContext()).getString(Const.PREFERENCE_USER_ID,""), 
        Utils.getSharedPreference(getApplicationContext()).getString(Const.PREFERENCE_MOBILE_NUMBER,"")); 
      call.enqueue(new Callback<ResponseBody>() { 
       @Override 
       public void onResponse(Call<List<Wallet>> call, Response<List<Wallet>> response) { 
        String responseString = null; 
        try { 

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

       @Override 
       public void onFailure(Call<List<Wallet>> call, Throwable t) { 
        Toast.makeText(Proceed_PaymentActivity.this, "Error", Toast.LENGTH_SHORT).show(); 
       } 
      });