UnknownHostException: name or service not known

后端 未结 3 2294
时光说笑
时光说笑 2021-02-20 08:28

I\'m attempting to return some data from an API using OkHttpClient in com.squareup.okhttp. I\'ve run into a few errors that i have eventually been able to overcome but i can\'t

相关标签:
3条回答
  • 2021-02-20 08:36

    For error like "java.net.UnknownHostException: [hostname]"

    The reason is your hostname is not in /etc/hosts, The solution is simple:

    sudo vim /etc/hosts
    

    change the line looks like:

    127.0.0.1  localhost
    

    to:

    127.0.0.1  [hostname] localhost
    

    Save and exit. If the problem still exist, may be you need to restart or run :

    sudo ifconfig eth0 down&&sudo ifconfig eth0 up
    

    Hope it can help you!

    0 讨论(0)
  • 2021-02-20 08:55

    Since you have not posted your code that how your connect() called, so please refer to my following working code

        private class StringRequest extends AsyncTask<Void, Void, String> {
            @Override
            protected String doInBackground(Void... voids) {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url("https://socialweb-analytics.lcloud.com/api/public/reports/jobs?companyKey=ato")
                        .addHeader("authorization", "Basic c2RidXNpbmVzc2FuYWx5dGljc0BhdG8uZ292LmF1OkFuYWx5dGljezEh")
                        .addHeader("cache-control", "no-cache")
                        .addHeader("postman-token", "65ef5553-77b5-053f-9c01-4fdf76bdc92f")
                        .build();
                try {
                    Response response = client.newCall(request).execute();
                    return response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                System.out.println(s);
            }
        }
    

    Then inside onCreate:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.textView);
        new StringRequest().execute();
    }
    

    My app run and got the following result (perhaps because of expired/invalid token):

    I/System.out: {"code":500,"message":"There was an error processing your request. It has been logged (ID a2f28b587b2f9dcc)."}
    

    P/S: make sure you have set <uses-permission android:name="android.permission.INTERNET" /> inside AndroidManifest.xml file

    0 讨论(0)
  • 2021-02-20 08:58

    Expanding on zhenbo xu's great answer this is what I did to automate things with sed in EC2.

    sudo sed -i -e '/127.0.0.1/ s/\(localhost\)/'$(hostname)' \1/' /etc/hosts

    Note that to allow variable expansion the single quotes end and restart around the subshell call.

    hostname is an executable on some Linux systems (but maybe not all, so you can use an environment variable instead,or remove the quotes around the subshell and use static string).

    The \1 is sed magic, it puts the "captured" search string (s/search/replacement/) in the escaped parentheses back into the replacement string.

    0 讨论(0)
提交回复
热议问题