0

I'm getting this error message "java.net.UnknownHostException: Unable to resolve host "api.themoviedb.org": No address associated with hostname". All the stackoverflow posts I read said that the manifest file might be wrong, but I think at this point it's right, so I'm not sure how to continue.

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.flixster">
<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/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>

MainActivity.java:

package com.example.flixster;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.codepath.asynchttpclient.AsyncHttpClient;
import com.codepath.asynchttpclient.callback.JsonHttpResponseHandler;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Headers;

 import static com.facebook.stetho.inspector.network.PrettyPrinterDisplayType.JSON;

 public class MainActivity extends AppCompatActivity {

    public static final String NOW_PLAYING_URL = 
   "https://api.themoviedb.org/3//movie/now_playing?api_key=a07e22bc18f5cb106bfe4cc1f83ad8ed";
    public static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // make get request on URL to get currently playing movies
    AsyncHttpClient client = new AsyncHttpClient();
    // JSON response handler since API returns JSON- deals with response success/failure
    client.get(NOW_PLAYING_URL, new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Headers headers, JsonHttpResponseHandler.JSON json) {
            Log.d(TAG, "onSuccess");
            JSONObject  jsonObject = json.jsonObject;

            try {
                JSONArray results = jsonObject.getJSONArray("result");
                Log.i(TAG, "Results: " + results.toString());
            } catch (JSONException e) {
                Log.e(TAG, "Hit json exception", e);
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(int statusCode, Headers headers, String response, Throwable throwable) {
            Log.d(TAG, "onFailure");
        }
    });
}

}

3
  • The DNS name resolves for me. (Not on Android ...) And indeed they are advertising both IPv4 and IPv6 addresses. Commented Feb 2, 2020 at 2:50
  • 2
    Other things to check. 1) Is your WiFi on? 2) Does your device have mobile internet enabled? 3) Is it in airplane mode? 4) Are you using an emulator? See also: stackoverflow.com/questions/6355498/… and similar Q&As Commented Feb 2, 2020 at 3:06
  • My test phone was indeed in airplane mode and didn't realize. Thank you very much @StephenC. Sometimes is the small things. Commented Dec 20, 2020 at 16:31

1 Answer 1

0

Looking at your code I found 2 issues:

  1. Your NOW_PLAYING_URL has an additional slash (themoviedb.org/3//movie/) after 3 so replace the string as

    public static final String NOW_PLAYING_URL = "https://api.themoviedb.org/3/movie/now_playing?api_key=a07e22bc18f5cb106bfe4cc1f83ad8ed";
    

That was the reason why your api call returns a failure.

  1. Next, the response is success but in your onSuccess() try block you are fetching data for "result" but your API response returns "results" so replace that key as below and you should not be having any issues.

    JSONArray results = jsonObject.getJSONArray("results");
    

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.