问题描述
我正在使用改型连接到heroku服务器。我的改造对象和MainActivity中的其他相关代码如下:
public class MainActivity extends AppCompatActivity {
private ApiService service;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OkHttpClient okHttpClient = new OkHttpClient();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://<my heroku app name>.herokuapp.com/")
.addConverterFactory(moshiConverterFactory.create())
.client(okHttpClient)
.build();
service = retrofit.create(ApiService.class);
}
public ApiService getService() {
return service;
}
我正在检查是否能够实现服务器连接的代码如下(但是,我认为这是可以的):
public class HomePage extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
final FragmentHomepageBinding binding = FragmentHomepageBinding.inflate(inflater,container,false);
final MainActivity mainActivity = (MainActivity) requireActivity(); //Casting
Call<ResponseBody> call = mainActivity.getService().getHome();
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call,Response<ResponseBody> response) {
if (!response.isSuccessful()) {
binding.tekstasB.setText("Code:"+response.code());
return;
}
ResponseBody result = response.body();
try {
Log.d("LoginPage",result.string());
binding.tekstasB.setText("RETROFIT WORKS");
} catch (IOException e) {
e.printstacktrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call,Throwable t) {
binding.tekstasB.setText(t.getMessage());
}
});
binding.listRecyclerFirst.setAdapter(new horizontallistAdapter());
return binding.getRoot();
}
}
此外,我的network_security_config.xml看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true"><my heroku app name>.herokuapp.com</domain>
</domain-config>
</network-security-config>
我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.<app name>">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:networkSecurityConfig="@xml/network_security_config"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<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>
取决于,如果使用http或https,我会收到错误消息: 网络安全配置不允许“与CLEARTEXT通信”,如果是https,我将得到“ CODE:404”
有人可以给我指导吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)