2017-10-13 78 views
0

在這段代碼中我將發送ID和mobile_no它檢索我的用戶平衡,但它不能在Toast打印。在rerofit我coundn't打印JSON

什麼我缺少正確這裏我的工作,但我沒有得到響應串

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

更新接口

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

我的JSON輸出

[ 
    { 
     "wallet_amt": 50 
    } 
] 

錯誤的logcat org.json.JSONException:值[{ 「wallet_amt」:50}]類型org.json.JSONArray的不能轉換到的JSONObject 10-13 12時39分:37.793 28522-28522/abc.example.com.rechargeappdesugn W/System.err:at org.json.JSON.typeMismatch(JSON.java:111) 10-13 12:39:37.793 28522-28522/abc.example .com.rechargeappdesugn W/System.err:at org.json.JSONObject。(JSONObject.java:160) 10-13 12:39:37.793 28522-28522/abc.example.com.rechargeappdesugn W/System.err:在org.json.JSONObject。(JSONObject.java:173) 12月10日至一十三日:39:37.793 28522-28522/abc.example.com.rechargeappdesugn W/System.err的:在abc.example.com.rechargeappdesugn.activity.Proceed_PaymentActivity $ 1.onResponse(Proceed_PaymentActivity.java:97) 10 -13 12:39:37.793 28522-28522/abc.example.com.rechargeappdesugn W/System.err的:在retrofit2.ExecutorCallAdapterFactory $ ExecutorCallbackCall $ 1 $ 1.run(ExecutorCallAdapterFactory.java:68) 十二月10日至13日:39:37.793 28522-28522/abc.example.com.rechargeappdesugn W/System.err:at android.os.Handler.handleCallback(Handler.java:751) 10-13 12:39:37.793 28522-28522/abc.example.com .rechargeappdesugn W/System.err的:在android.os.Handler.dispatchMessage(Handler.java:95) 十二月10日至13日:39:37.793 28522-28522/abc.example.com.rechargeappdesugn W/System.err的:在android.os.Looper.loop(Looper.java:154) 10-13 12:39:37.793 28522-28522/abc.example.com.rechargeappd esugn W/System.err:at android.app.ActivityThread.main(ActivityThread.java:6119) 10-13 12:39:37.793 28522-28522/abc.example.com.rechargeappdesugn W/System.err:at java .lang.reflect.Method.invoke(本機方法) 十二月10日至13日:39:37.793 28522-28522/abc.example.com.rechargeappdesugn W/System.err的:在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller .RUN(ZygoteInit.java:886) 一十二月10日至13日:39:37.794 28522-28522/abc.example.com.rechargeappdesugn W/System.err的:在com.android.internal.os.ZygoteInit.main(ZygoteInit。 java:776)

+0

它拋出JsonParsingException。檢查你的logcat – Kuls

+0

是否有任何錯誤消息/異常拋出? –

回答

2

您的jsonJSONArray

在代碼中使用JSONArray jsonArray = new JSONArray(responseString);

您必須使用getString()而不是opString()。

@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 = new JSONArray(responseString); JSONObject jsonObject = jsonArray.getJSONObject(0); //這裏我得到一個值爲0或null它沒有顯示任何東西Utils.getSharedPreference(getApplicationContext())。edit()。putString(Const.PREFERENCE_Walllet_Amt,jsonObject.optString(「wallet_amt」)); –

0

首先你不需要做相關回應任何數據操作的改裝在內部處理這個的。 請使用下面的代碼片段來使它工作。

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