在使用 Android 中的 Retrofit 调用实时货币转换 API 时出现错误。

huangapple go评论76阅读模式
英文:

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 = &quot;MainActivity&quot;;
private TextView result;
private EditText currency;
private Button button;
private static String BASE_URL = &quot;https://api.currencyscoop.com/v1/&quot;;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, &quot;onCreate: called&quot;);
initViews();
button.setOnClickListener(this);
}
public void initViews(){
Log.d(TAG, &quot;initViews: called&quot;);
result = findViewById(R.id.result);
currency = findViewById(R.id.amount);
button = findViewById(R.id.button);
}
@Override
public void onClick(View v) {
Log.d(TAG, &quot;onClick: called&quot;);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitClient retrofitClient = retrofit.create(RetrofitClient.class);
Call&lt;ConvertAmt&gt; calling = retrofitClient.getConvertedValue(&quot;my_api_key&quot;,&quot;USD&quot;, &quot;INR&quot;, currency.getText().toString());
calling.enqueue(new Callback&lt;ConvertAmt&gt;() {
@Override
public void onResponse(Call&lt;ConvertAmt&gt; call, Response&lt;ConvertAmt&gt; response) {
Log.d(TAG, &quot;onResponse: called :----------------------&gt; &quot;+response.body().getResult());
}
@Override
public void onFailure(Call&lt;ConvertAmt&gt; call, Throwable t) {
Log.d(TAG, &quot;onFailure: --------------&gt; &quot;+t.getStackTrace());
}
});
}

}

// This is my RetrofitClient interface

public interface RetrofitClient {
@GET(&quot;/convert?&quot;)
Call&lt;ConvertAmt&gt; getConvertedValue(@Query(&quot;api_key&quot;) String api_key, @Query(&quot;base&quot;) String base, @Query(&quot;to&quot;) String to, @Query(&quot;amount&quot;) 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 &quot;ConvertAmt{&quot; +
&quot;result=&quot; + result +
&#39;}&#39;;
}

}

// 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

{
&quot;meta&quot;: {
&quot;code&quot;: 200,
&quot;disclaimer&quot;: &quot;Usage subject to terms: https://currencyscoop.com/terms&quot;
},
&quot;response&quot;: {
&quot;timestamp&quot;: 1598589131,
&quot;date&quot;: &quot;2020-08-27&quot;,
&quot;from&quot;: &quot;USD&quot;,
&quot;to&quot;: &quot;INR&quot;,
&quot;amount&quot;: &quot;10&quot;,
&quot;value&quot;: 738.1371942
}
}

So you can convert your pojo to

public class ConvertAmt { 
private Map&lt;String,Object&gt; meta;
private Map&lt;String,Object&gt; response;
}

and you can get the response like this

Call&lt;ConvertAmt&gt; calling = retrofitClient.getConvertedValue(&quot;my_api_key&quot;,&quot;USD&quot;, &quot;INR&quot;, currency.getText().toString());
calling.getResponse().get(&quot;amount&quot;);

huangapple
  • 本文由 发表于 2020年8月28日 02:31:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63622207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定