0

I am using an android emulator on a desktop. I am getting the following error when I try to connect to my backend.

java.net.UnknownHostException: Unable to resolve host
"<my host url>": No address associated with hostname

Here are the things I tried.

My android manifest has the following permissions

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

I verified that I am able to reach the url from the browser inside the emulator. It's slow, but it resolves and connects.

This is my dev env setup

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'

What are the possible culprits I need to investigate?

Here's the code for 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);
                    }
                });
    }

}

And here's the retrofit code

//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();

}

And here's the entry in the strings.xml

<string name="backend_url">http://mywebsite.net</string>
horatius
  • 784
  • 1
  • 12
  • 30
  • try with `IP address` and see if that works, Also please share your code so we can take a look at it. – Alireza Ahmadi Jul 08 '18 at 14:22
  • I tried. Still gives me the same error. I already mentioned that it works in the browser on the emulator, so DNS resolution is not the issue. – horatius Jul 10 '18 at 11:24

1 Answers1

0

For emulators, following configurations are available depending on the server:-

  • Server on Local Machine:- 10.0.2.2 (which represents the localhost from emulators perspective)
  • Server on the Machine in Local Network: Assigned IP Address (In the above two cases make sure that the emulator is connected to the same network)
  • If your services are hosted on the remote server:- In this case you must be having a pre-determined endpoint, which must be used.

If the above solution fails try the same approach with a different emulator or a physical device.

I assume the URL you provided here is a placeholder, and you are trying to place the URL of your own website, but if it is the actual URL you are using, it seems to be redirecting to a different page. If redirection is the issue I recommend placing the original URL of the website instead of a forwarding address.

I hope this helps.

Sankalp
  • 1,417
  • 1
  • 12
  • 20
  • I have a predetermined public ip that I am using as the backend. The backend is accessible on the chrome app on the emulator. I want to use the desktop to run android development. My laptop is not all that powerful. – horatius Jul 12 '18 at 10:27