简体   繁体   English

java.net.UnknownHostException:无法解析主机“api.themoviedb.org”

[英]java.net.UnknownHostException: Unable to resolve host "api.themoviedb.org

I'm getting this error message "java.net.UnknownHostException: Unable to resolve host "api.themoviedb.org": No address associated with hostname".我收到此错误消息“java.net.UnknownHostException:无法解析主机“api.themoviedb.org”:没有与主机名关联的地址”。 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.我读过的所有stackoverflow帖子都说清单文件可能是错误的,但我认为此时是正确的,所以我不确定如何继续。

AndroidManifest.xml: 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:主活动.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");
        }
    });
}

} }

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您的NOW_PLAYING_URL在 3 之后有一个额外的斜线( themoviedb.org/3//movie/ ),因此将字符串替换为

    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.这就是您的 api 调用返回失败的原因。

  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.接下来,响应成功,但在您的onSuccess() try块中,您正在获取"result"数据,但您的 API 响应返回"results"因此请按如下所示替换该键,您应该不会遇到任何问题。

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

Hope this helps.希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:java.net.UnknownHostException:无法解析主机“api.themoviedb.org” - Error: java.net.UnknownHostException: Unable to resolve host "api.themoviedb.org java.net.UnknownHostException:无法解析主机没有与主机名关联的地址 - java.net.UnknownHostException: Unable to resolve host No address associated with hostname Android API 23 java.net.UnknownHostException:无法解析主机“URL”:没有与主机名关联的地址 - Android API 23 java.net.UnknownHostException: Unable to resolve host “URL”: No address associated with hostname Android 6.0:java.net.UnknownHostException:无法解析主机。 在Android 7.0上正常工作 - Android 6.0 : java.net.UnknownHostException: Unable to resolve host. Working fine in Android 7.0 java.net.UnknownHostException:无法解析主机“ s3-ap-northeast-1.amazonaws.com”:没有与主机名关联的地址 - java.net.UnknownHostException: Unable to resolve host “s3-ap-northeast-1.amazonaws.com”: No address associated with hostname :java.net.UnknownHostException:无法解析主机“hosturl”:没有与主机名关联的地址 - : java.net.UnknownHostException: Unable to resolve host “hosturl”: No address associated with hostname java.net.UnknownHostException:无法解析主机“@MYIPADDRESS,当使用android + mongodb时 - java.net.UnknownHostException: Unable to resolve host "@MYIPADDRESS, when using android + mongodb 重新启动java.net时android应用程序崩溃。UnknownHostException:无法解析主机 - android app crashes on reboot java.net.UnknownHostException: Unable to resolve host java.net.UnknownHostException:无法解析主机“www.google.com” - java.net.UnknownHostException: Unable to resolve host “www.google.com” java.net.UnknownHostException:无法解析主机“<url> &quot;: 没有与主机名和字符 0 处的输入结束关联的地址 - java.net.UnknownHostException: Unable to resolve host "<url>": No address associated with hostname and End of input at character 0 of
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM