我正在尝试构建一个转换货币的应用程序。我使用公共 API 来访问货币兑换功能。当我调试我的代码时,由于这个网络安全错误,我无法查看收到的 JSON 对象。我在这里查看了其他帖子,并尝试通过在 AndriodManifest.xml 文件中包含android:usesCleartextTraffic="true"和uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"来修复它。这是调试输出的图像:image
这是我的代码文件。
AndriodManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.currencyconversion">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.CurrencyConversion"
android:usesCleartextTraffic="true"
>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java:
package com.example.currencyconversion;
import androidx.appcompat.app.AppCompatActivity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import com.example.currencyconversion.Retrofit.RetrofitBuilder;
import com.example.currencyconversion.Retrofit.RetrofitInterface;
import com.google.gson.JsonObject;
public class MainActivity extends AppCompatActivity {
Button button;
EditText currencyToBeConverted;
EditText currencyConverted;
Spinner convertToDropdown;
Spinner convertFromDropdown;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialization
currencyConverted =(EditText) findViewById(R.id.currency_converted);
currencyToBeConverted =(EditText) findViewById(R.id.currency_to_be_converted);
convertToDropdown = (Spinner) findViewById(R.id.convert_to);
convertFromDropdown = (Spinner) findViewById(R.id.convert_from);
button = (Button) findViewById(R.id.button);
// Adding Functionality
String[] downDownList = {"USD", "AED", "EUR", "GBP"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, downDownList);
convertToDropdown.setAdapter(adapter);
convertFromDropdown.setAdapter(adapter);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
//API Call
RetrofitInterface retrofitInterface = RetrofitBuilder.getRetrofitInstance().create(RetrofitInterface.class);
//Call<JsonObject> call = retrofitInterface.getExchangeCurrency(convertFromDropdown.getSelectedItem().toString());
Call<JsonObject> call = retrofitInterface.getExchangeCurrency("AED");
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
Log.d("response", String.valueOf(response.body())); //HERE IS WHERE I DEBUG
/*JsonObject res = response.body();
JsonObject rates = res.getAsJsonObject("conversion_rates");
double currency = Double.valueOf(currencyToBeConverted.getText().toString());
double multiplier = Double.valueOf(rates.get(convertToDropdown.getSelectedItem().toString()).toString());
double result = currency * multiplier;
currencyConverted.setText(String.valueOf(result));*/
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
}
});
}
});
}
RetrofitBuilder.java:
package com.example.currencyconversion.Retrofit;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitBuilder {
public static Retrofit retrofit;
public static Retrofit getRetrofitInstance() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl("https://v6.exchangerate-api.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
RetrofitInterface.java:
package com.example.currencyconversion.Retrofit;
import com.google.gson.JsonObject;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
public interface RetrofitInterface {
@GET("v6/4API-KEY/latest/{currency}")
Call<JsonObject> getExchangeCurrency(@Path("currency") String currency);
}