我在桌面上使用安卓模拟器。尝试连接到后端时出现以下错误。
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>