1. Download our Official Android App: Forums for Android!

“InetAddress” don't work in Android but it works in netbeans

Discussion in 'Android Development' started by akir, Sep 23, 2017.

  1. akir

    akir Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    4
    Joined:
    Mar 16, 2017

    Mar 16, 2017
    4
    0
    5
    Goal:
    Use host adress using InetAddress.

    Problem:
    Why does the code not working in android studio? The same syntax code works at at netbeans.

    What part am I missing?

    Thank you!

    Info:
    *I'm new in android
    *I'm using API 23
    *I also have tried "getByName" but it doesn't work


    [​IMG] [​IMG]


    AndroidManifest.xml


    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    3.     package="com.jfdimarzio.test1">
    4.  
    5.     <application
    6.         android:allowBackup="true"
    7.         android:icon="@mipmap/ic_launcher"
    8.         android:label="@string/app_name"
    9.         android:roundIcon="@mipmap/ic_launcher_round"
    10.         android:supportsRtl="true"
    11.         android:theme="@style/AppTheme">
    12.         <activity android:name=".MainActivity">
    13.             <intent-filter>
    14.                 <action android:name="android.intent.action.MAIN" />
    15.  
    16.                 <category android:name="android.intent.category.LAUNCHER" />
    17.             </intent-filter>
    18.         </activity>
    19.     </application>
    20.  
    21.     <uses-permission android:name="android.permission.INTERNET" />
    22.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    23.  
    24. </manifest>

    MainActivity.java


    Code (Java):
    1. package com.jfdimarzio.test1;
    2.  
    3. import android.support.v7.app.AppCompatActivity;
    4. import android.os.Bundle;
    5.  
    6. public class MainActivity extends AppCompatActivity {
    7.  
    8.     @Override
    9.     protected void onCreate(Bundle savedInstanceState) {
    10.         super.onCreate(savedInstanceState);
    11.         setContentView(R.layout.activity_main);
    12.     }
    13. }
    Client.java

    Code (Java):
    1. package com.jfdimarzio.test1;
    2.  
    3. import java.net.InetAddress;
    4.  
    5.  
    6. public class Client
    7. {
    8.  
    9.     private static final int PORT = 2001;
    10.     private static final String DEFAULT_HOST = "www.google.com";
    11.  
    12.     public void test()
    13.     {
    14.  
    15.         java.net.InetAddress addr;
    16.         addr = InetAddress.getAllByName(DEFAULT_HOST);
    17.  
    18.     }
    19. }
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,896
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,896
    11,572
    1,988
    Male
    Software developer
    South West of England
    You need to read about catching exceptions in Java. It's fundamental.
    The reason you're getting this error is because method getAllByName() throws a checked exception. That means you have to write code which handles the exception at run time. Do you know what an exception is?
    This is done by using a 'try' block, followed by one or more 'catch' blocks.

    I could give you the code, but I don't think you'd learn anything. Here's a web page which explains all about exceptions, and how to handle them -

    https://beginnersbook.com/2013/04/java-exception-handling/

    See if you can work out what code you need to add, to handle the exception.

    btw you can look at the Javadocs for this method here

    https://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html#getAllByName(java.lang.String)

    Note the method signature - it declares a thrown exception (UnknownHostException). Your code has to catch and deal with that exception.
     
    scary alien likes this.

Share This Page

Loading...