英文:
Getting error while using live currency conversion API by Retrofit in Android
问题
以下是翻译好的部分:
// 这是我的 MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private static final String TAG = "MainActivity";
private TextView result;
private EditText currency;
private Button button;
private static String BASE_URL = "https://api.currencyscoop.com/v1/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: 被调用");
initViews();
button.setOnClickListener(this);
}
public void initViews(){
Log.d(TAG, "initViews: 被调用");
result = findViewById(R.id.result);
currency = findViewById(R.id.amount);
button = findViewById(R.id.button);
}
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: 被调用");
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitClient retrofitClient = retrofit.create(RetrofitClient.class);
Call<ConvertAmt> calling = retrofitClient.getConvertedValue("my_api_key","USD", "INR", currency.getText().toString());
calling.enqueue(new Callback<ConvertAmt>() {
@Override
public void onResponse(Call<ConvertAmt> call, Response<ConvertAmt> response) {
Log.d(TAG, "onResponse: 被调用 :----------------------> "+response.body().getResult());
}
@Override
public void onFailure(Call<ConvertAmt> call, Throwable t) {
Log.d(TAG, "onFailure: --------------> "+t.getStackTrace());
}
});
}
}
// 这是我的 RetrofitClient 接口
public interface RetrofitClient {
@GET("/convert?")
Call<ConvertAmt> getConvertedValue(@Query("api_key") String api_key, @Query("base") String base, @Query("to") String to, @Query("amount") String amount);
}
// 这是我的 ConvertAmt 类(存储结果的模型类)
public class ConvertAmt {
private double result;
public ConvertAmt(double result) {
this.result = result;
}
public double getResult() {
return result;
}
public void setResult(double result) {
this.result = result;
}
@Override
public String toString() {
return "ConvertAmt{" +
"result=" + result +
'}';
}
}
// 这是我的 StackTrace
2020-08-27 23:59:53.212 1471-1471/com.example.online_currency W/System.err: java.io.EOFException: 输入结束于第1行第1列路径 $
2020-08-27 23:59:53.212 1471-1471/com.example.online_currency W/System.err: at com.google.gson.stream.JsonReader.nextNonWhitespace(JsonReader.java:1401)
2020-08-27 23:59:53.212 1471-1471/com.example.online_currency W/System.err: at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:549)
2020-08-27 23:59:53.212 1471-1471/com.example.online_currency W/System.err: at com.google.gson.stream.JsonReader.peek(JsonReader.java:425)
2020-08-27 23:59:53.212 1471-1471/com.example.online_currency W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:205)
2020-08-27 23:59:53.212 1471-1471/com.example.online_currency W/System.err: at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:39)
2020-08-27 23:59:53.212 1471-1471/com.example.online_currency W/System.err: at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27)
2020-08-27 23:59:53.213 1471-1471/com.example.online_currency W/System.err: at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:223)
2020-08-27 23:59:53.213 1471-1471/com.example.online_currency W/System.err: at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:121)
2020-08-27 23:59:53.213 1471-1471/com.example.online_currency W/System.err: at okhttp3.RealCall$AsyncCall.execute(RealCall.java:206)
2020-08-27 23:59:53.213 1471-1471/com.example.online_currency W/System.err: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
2020-08-27 23:59:53.213 1471-1471/com.example.online_currency W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
2020-08-27 23:59:53.213 1471-1471/com.example.online_currency W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
2020-08-27 23:59:53.213 1471-1471/com.example.online_currency W/System.err: at java.lang.Thread.run(Thread.java:761)
英文:
// This is my MainActivity.java
ublic class MainActivity extends AppCompatActivity implements View.OnClickListener{
private static final String TAG = "MainActivity";
private TextView result;
private EditText currency;
private Button button;
private static String BASE_URL = "https://api.currencyscoop.com/v1/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: called");
initViews();
button.setOnClickListener(this);
}
public void initViews(){
Log.d(TAG, "initViews: called");
result = findViewById(R.id.result);
currency = findViewById(R.id.amount);
button = findViewById(R.id.button);
}
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: called");
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitClient retrofitClient = retrofit.create(RetrofitClient.class);
Call<ConvertAmt> calling = retrofitClient.getConvertedValue("my_api_key","USD", "INR", currency.getText().toString());
calling.enqueue(new Callback<ConvertAmt>() {
@Override
public void onResponse(Call<ConvertAmt> call, Response<ConvertAmt> response) {
Log.d(TAG, "onResponse: called :----------------------> "+response.body().getResult());
}
@Override
public void onFailure(Call<ConvertAmt> call, Throwable t) {
Log.d(TAG, "onFailure: --------------> "+t.getStackTrace());
}
});
}
}
// This is my RetrofitClient interface
public interface RetrofitClient {
@GET("/convert?")
Call<ConvertAmt> getConvertedValue(@Query("api_key") String api_key, @Query("base") String base, @Query("to") String to, @Query("amount") String amount);
}
// This is my ConvertAmt class (Model class where i am storing the result)
public class ConvertAmt {
private double result;
public ConvertAmt(double result) {
this.result = result;
}
public double getResult() {
return result;
}
public void setResult(double result) {
this.result = result;
}
@Override
public String toString() {
return "ConvertAmt{" +
"result=" + result +
'}';
}
}
// This is my StackTrace
2020-08-27 23:59:53.212 1471-1471/com.example.online_currency W/System.err: java.io.EOFException: End of input at line 1 column 1 path $
2020-08-27 23:59:53.212 1471-1471/com.example.online_currency W/System.err: at com.google.gson.stream.JsonReader.nextNonWhitespace(JsonReader.java:1401)
2020-08-27 23:59:53.212 1471-1471/com.example.online_currency W/System.err: at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:549)
2020-08-27 23:59:53.212 1471-1471/com.example.online_currency W/System.err: at com.google.gson.stream.JsonReader.peek(JsonReader.java:425)
2020-08-27 23:59:53.212 1471-1471/com.example.online_currency W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:205)
2020-08-27 23:59:53.212 1471-1471/com.example.online_currency W/System.err: at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:39)
2020-08-27 23:59:53.212 1471-1471/com.example.online_currency W/System.err: at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27)
2020-08-27 23:59:53.213 1471-1471/com.example.online_currency W/System.err: at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:223)
2020-08-27 23:59:53.213 1471-1471/com.example.online_currency W/System.err: at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:121)
2020-08-27 23:59:53.213 1471-1471/com.example.online_currency W/System.err: at okhttp3.RealCall$AsyncCall.execute(RealCall.java:206)
2020-08-27 23:59:53.213 1471-1471/com.example.online_currency W/System.err: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
2020-08-27 23:59:53.213 1471-1471/com.example.online_currency W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
2020-08-27 23:59:53.213 1471-1471/com.example.online_currency W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
2020-08-27 23:59:53.213 1471-1471/com.example.online_currency W/System.err: at java.lang.Thread.run(Thread.java:761)
答案1
得分: 0
以下是翻译好的内容:
似乎你从响应中期望的数据与你正在映射的 POJO(Plain Old Java Object,普通的 Java 对象)不匹配。这是你期望的样子:
public class ConvertAmt {
private double result;
}
实际的响应是这样的:
{
"meta": {
"code": 200,
"disclaimer": "Usage subject to terms: https://currencyscoop.com/terms"
},
"response": {
"timestamp": 1598589131,
"date": "2020-08-27",
"from": "USD",
"to": "INR",
"amount": "10",
"value": 738.1371942
}
}
因此,你可以将你的 POJO 转换为这样:
public class ConvertAmt {
private Map<String,Object> meta;
private Map<String,Object> response;
}
你可以这样获取响应:
Call<ConvertAmt> calling = retrofitClient.getConvertedValue("my_api_key", "USD", "INR", currency.getText().toString());
calling.getResponse().get("amount");
英文:
It seems the data you are expecting from the response is not matching to the pojo that you are mapping
this is what you are expecting,
public class ConvertAmt {
private double result;
}
actual response is
{
"meta": {
"code": 200,
"disclaimer": "Usage subject to terms: https://currencyscoop.com/terms"
},
"response": {
"timestamp": 1598589131,
"date": "2020-08-27",
"from": "USD",
"to": "INR",
"amount": "10",
"value": 738.1371942
}
}
So you can convert your pojo to
public class ConvertAmt {
private Map<String,Object> meta;
private Map<String,Object> response;
}
and you can get the response like this
Call<ConvertAmt> calling = retrofitClient.getConvertedValue("my_api_key","USD", "INR", currency.getText().toString());
calling.getResponse().get("amount");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论