【问题标题】:Getting error while using live currency conversion API by Retrofit in Android在 Android 中通过 Retrofit 使用实时货币转换 API 时出错
【发布时间】:2020-12-16 16:54:24
【问题描述】:

// 这是我的 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());
        }
    });
}

}

// 这是我的 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 类(我存储结果的模型类)

公共类 ConvertAmt { 私人双重结果;

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: 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:在 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: 在 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:在 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:在 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: 在 retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:39) 2020-08-27 23:59:53.212 1471-1471/com.example.online_currency W/System.err: 在 retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27) 2020-08-27 23:59:53.213 1471-1471/com.example.online_currency W/System.err: 在 retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:223) 2020-08-27 23:59:53.213 1471-1471/com.example.online_currency W/System.err: 在改造 2.OkHttpCall$1.onResponse(OkHttpCall.java:121) 2020-08-27 23:59:53.213 1471-1471/com.example.online_currency W/System.err: 在 okhttp3.RealCall$AsyncCall.execute(RealCall.java:206) 2020-08-27 23:59:53.213 1471-1471/com.example.online_currency W/System.err: 在 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)

【问题讨论】:

    标签: java android android-studio http retrofit


    【解决方案1】:

    您期望从响应中获得的数据似乎与您正在映射的 pojo 不匹配 这是你所期待的,

    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");
    

    【讨论】:

    • 好的,现在完全搞定了,我的 RetrofitClient 界面呢!好吗
    • 嘿@saifali 我刚刚得到了我想要的输出,但它显示了几秒钟“:没有指定网络安全配置,使用平台默认值”然后我得到输出.....怎么办,但它不是与网络有关的错误
    猜你喜欢
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多