我正在台式机上使用android模拟器。当我尝试连接到后端时遇到以下错误。
java.net.UnknownHostException: Unable to resolve host
"<my host url>": No address associated with hostname
这是我尝试过的事情。
我的android清单具有以下权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
我确认可以从模拟器内的浏览器访问该URL。它很慢,但是可以解析并连接。
这是我的开发环境设置
Android studio version: 3.1.3 Android emulator device: Pixel 2 XL API 27 gradle version: 4.4 //Android androidBuildToolsVersion = '27.0.2' androidMinSdkVersion = 19 androidTargetSdkVersion = 27 androidCompileSdkVersion = 27 //Libraries butterKnifeVersion = '8.4.0' rxJavaAdapterVersion = '2.1.0' rxAndroidVersion = '1.2.1' rxJavaVersion = '1.3.0' rxBindingVersion = '2.0.0' javaxAnnotationVersion = '1.0' javaxInjectVersion = '1' gsonVersion = '2.1.0' okHttpVersion = '3.8.0' retrofitVersion = '2.3.0' androidAnnotationsVersion = '27.0.2' materialSearchViewVersion = '1.4.0' materialDesignDimensVersion = '1.4' materializeVersion = '1.0.0@aar' constraintLayoutVersion = '1.0.2' daggerVersion = '2.11' nineoldandroidsVersion = '2.4.0' supportAppCompat = '27.0.2' lombokVersion= '1.16.20'
我需要调查哪些罪魁祸首?
这是SignUpActivity的代码
public class SignInActivity extends AppCompatActivity {
@Inject
@Named("default")
Retrofit retrofit;
@BindView(R.id.username_text_input_layout)
TextInputLayout usernameInputLayout;
@BindView(R.id.password_input_layout)
TextInputLayout passwordInputLayout;
@Nullable
@BindView(R.id.image_button)
ImageView submitImageView;
@BindView(R.id.forgot_password)
TextView forgotPassword;
@BindView(R.id.email_editext_sign_in)
EditText emailEditTextSignIn;
@BindView(R.id.password_editext_sign_in)
EditText passwordEditTextSignIn;
@BindView(R.id.card_view_sign_in)
CardView cardViewSignIn;
@BindView(R.id.card_view_sign_up)
CardView cardViewSignUp;
String emailText;
private RestApi restApi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
((MyApplication) getApplication()).getSigninUserNetworkComponent().inject(this);
restApi = retrofit.create(RestApi.class);
ButterKnife.bind(this);
ActivityUtil.setCollapsedHintMiddle(usernameInputLayout, this);
ActivityUtil.setCollapsedHintMiddle(passwordInputLayout, this);
}
@OnClick({R.id.forgot_password, R.id.image_button, R.id.card_view_sign_in, R.id.card_view_sign_up})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.forgot_password:
Intent intent = new Intent(SignInActivity.this, AuthForgotPasswordActivity.class);
startActivity(intent);
overridePendingTransition(R.animator.swipe_up_animation, R.animator.no_change);
break;
case R.id.image_button:
emailText = emailEditTextSignIn.getText().toString();
if (emailText.isEmpty()) {
emailEditTextSignIn.setError("Email address cannot be empty");
} else {
Toast.makeText(this, R.string.signed_toast_message, Toast.LENGTH_SHORT).show();
getSignInResponse(emailText);
}
break;
}
}
private void getSignInResponse(String emailAddress) {
restApi.getUser(emailAddress)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Response<SignInModel>>() {
@Override
public void onCompleted() {
Log.e("", "onCompleted: ");
}
@Override
public void onError(Throwable e) {
Log.e("", "onError: " + e);
}
@Override
public void onNext(Response<SignInModel> jsonObjectResponse) {
Log.e("", "onNext: " + jsonObjectResponse);
UIDisplayUtil.saveSignInUserDetails(SignInActivity.this, jsonObjectResponse.body());
Intent intentSubmit = new Intent(SignInActivity.this, SwipePageActivity.class);
startActivity(intentSubmit);
}
});
}
}
这是改造代码
//todo:combine these two url feed and Football Data
@Singleton
@Provides
@Named("default")
public Retrofit getRetrofit(Gson gson) {
return new Retrofit.Builder()
.baseUrl(MyApplication.getContext().getString(R.string.backend_url))
.client(okHttpClient)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(new NullOnEmptyConverterFactory())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
这是strings.xml中的条目
<string name="backend_url">http://mywebsite.net</string>
答案 0 :(得分:0)
对于仿真器,根据服务器的不同,可以使用以下配置:-
如果上述解决方案失败,请在其他仿真器或物理设备上尝试相同的方法。
我假设您在此处提供的URL是一个占位符,并且您正在尝试放置自己网站的URL,但是如果它是您正在使用的实际URL,则它似乎将重定向到另一个页面。如果重定向是问题,我建议放置网站的原始URL而不是转发地址。
我希望这会有所帮助。