在改造中我无法打印 JSON
in rerofit i coundn't print JSON
在这段代码中,我将发送 id,mobile_no 它会检索我的用户余额,但无法在 Toast
.
中打印
我在这里错过了什么我的工作正常但我没有收到响应字符串
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 Interface
@FormUrlEncoded
@POST("XXXX")
Call<ResponseBody> getWalle_Data(@Field("id") String uid,@Field("mobile") String mobile);
My JSON Output
[
{
"wallet_amt": 50
}
]
Error Logcat
org.json.JSONException: Value [{"wallet_amt":50}] of type org.json.JSONArray cannot be converted to 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: at org.json.JSONObject.(JSONObject.java:173)
10-13 12:39:37.793 28522-28522/abc.example.com.rechargeappdesugn W/System.err: at abc.example.com.rechargeappdesugn.activity.Proceed_PaymentActivity.onResponse(Proceed_PaymentActivity.java:97)
10-13 12:39:37.793 28522-28522/abc.example.com.rechargeappdesugn W/System.err: at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.run(ExecutorCallAdapterFactory.java:68)
10-13 12: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: at android.os.Handler.dispatchMessage(Handler.java:95)
10-13 12:39:37.793 28522-28522/abc.example.com.rechargeappdesugn W/System.err: at android.os.Looper.loop(Looper.java:154)
10-13 12:39:37.793 28522-28522/abc.example.com.rechargeappdesugn 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(Native Method)
10-13 12:39:37.793 28522-28522/abc.example.com.rechargeappdesugn 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.rechargeappdesugn W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
你的 json
是 JSONArray
.
在您的代码中使用 JSONArray jsonArray = new JSONArray(responseString);
。
you have to use getString() instead of 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();
}
}
首先,您不需要进行任何与响应相关的数据操作,因为改造会在内部进行处理。
请使用以下代码片段使其正常工作。
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();
}
});
在这段代码中,我将发送 id,mobile_no 它会检索我的用户余额,但无法在 Toast
.
我在这里错过了什么我的工作正常但我没有收到响应字符串
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 Interface
@FormUrlEncoded
@POST("XXXX")
Call<ResponseBody> getWalle_Data(@Field("id") String uid,@Field("mobile") String mobile);
My JSON Output
[
{
"wallet_amt": 50
}
]
Error Logcat org.json.JSONException: Value [{"wallet_amt":50}] of type org.json.JSONArray cannot be converted to 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: at org.json.JSONObject.(JSONObject.java:173) 10-13 12:39:37.793 28522-28522/abc.example.com.rechargeappdesugn W/System.err: at abc.example.com.rechargeappdesugn.activity.Proceed_PaymentActivity.onResponse(Proceed_PaymentActivity.java:97) 10-13 12:39:37.793 28522-28522/abc.example.com.rechargeappdesugn W/System.err: at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.run(ExecutorCallAdapterFactory.java:68) 10-13 12: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: at android.os.Handler.dispatchMessage(Handler.java:95) 10-13 12:39:37.793 28522-28522/abc.example.com.rechargeappdesugn W/System.err: at android.os.Looper.loop(Looper.java:154) 10-13 12:39:37.793 28522-28522/abc.example.com.rechargeappdesugn 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(Native Method) 10-13 12:39:37.793 28522-28522/abc.example.com.rechargeappdesugn 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.rechargeappdesugn W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
你的 json
是 JSONArray
.
在您的代码中使用 JSONArray jsonArray = new JSONArray(responseString);
。
you have to use getString() instead of 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();
}
}
首先,您不需要进行任何与响应相关的数据操作,因为改造会在内部进行处理。 请使用以下代码片段使其正常工作。
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();
}
});