2016-12-07 33 views
0

我想从ipify使用API​​获取android设备的IP地址。我尝试使用https://www.ipify.org/中建议的API。很简单。使用API​​获取Android设备的IP地址

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="com.example.ramesh.getipaddress"> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 
    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     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><!-- ATTENTION: This was auto-generated to add Google Play services to your project for 
    App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. --> 
     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version"/> 

    </application> 

</manifest> 



import android.net.Uri; 
import android.net.wifi.WifiManager; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.View; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLConnection; 


public void onClick(View v) { 


     Log.i("Test", "End"); 

     new networkingTask().execute(); 

     //getContents("http://localhost:8080"); 


     Log.i("Error", "Error1"); 
    } 

} 

class networkingTask extends AsyncTask<String, String,String> { 
    protected String doInBackground(String... urls) { 
     //now call your ipify method which is doing the networking or calling a method that might be doing the networkign 

     //getContents("http://localhost:8080"); 

     getipify(); 

     return null; 
    } 

    public void getipify() { 

     try (java.util.Scanner s = new java.util.Scanner(new java.net.URL("https://api.ipify.org").openStream(), "UTF-8").useDelimiter("\\A")) { 
      System.out.println("My current IP address is " + s); 
     } catch (java.io.IOException e) { 
      System.out.println("======"); 
      System.out.println(e); 
      System.out.println("======"); 

     } 


     Log.i("Test","====TEST===="); 
    } 

} 

由于某种原因,它不会打开流。我得到的错误

java.lang.IllegalStateException:在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick

我已经使用这个方法迅速和它的作品的onClick:为Android无法执行方法正好。我已经看过所有其他方法,但看起来很简单。从Android Studio中

错误部分: 碰撞 E/AndroidRuntime的---------开头:致命异常:主 工艺:com.example.ramesh.getipaddress,PID:2381 java.lang中.RuntimeException:无法启动活动ComponentInfo {com.example.ramesh.getipaddress/com.example.ramesh.getipaddress.MainActivity}:android.os.NetworkOnMainThreadException在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.access $ 800(ActivityThread.java:151) at android.app.ActivityThread $ H .handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main (ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os。 ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 产生的原因:在android.os.StrictMode $ AndroidBlockGuardPolicy android.os.NetworkOnMainThreadException .onNetwork(StrictMode.java:1147) at java.net.InetAddress.lookupHostByName(InetAddr ess.java:418) at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)

任何帮助将不胜感激。

感谢

拉梅什

+0

问题似乎没有与此API,但它似乎与您的onClickListener。请提供您收到错误的完整代码和行号。 – nitinsh99

+0

根据请求添加了代码的其余部分和错误部分。感谢你的帮助。 –

回答

0

为完整的代码去看起来像你的acitivty崩溃,因为你正试图执行主线程上的网络电话。您必须在后台线程中进行联网。你可以和应该使用异步任务来实现这一目标:

你的代码看起来就像这样:

更新:对于第二个错误:这样做:

// just make sure to replace button with the id of your button 
Button b = (Button) findViewById(R.id.button); 

    b.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
     new networkingTask().execute(); 
     } 
    }); 

public void onClick(View v) { 

     Log.i("Test", "End"); 
     Log.i("Error", "Error1"); 

new networkingTask().execute(); 

    } 

//创建的AsyncTask类:

private class networkingTask extends AsyncTask<String, String,String> { 
    protected String doInBackground(String... urls) { 
     //now call your ipify method which is doing the networking or calling a method that might be doing the networkign 

     //getContents("http://localhost:8080"); 

        getipify(); 

     return null; 
    } 

} 
+0

致命例外:main 进程:com.example.ramesh.getmyip1,PID:2561 java.lang.IllegalStateException:无法在父级或祖先上查找方法onClick(MainActivity)(View)上下文中定义的android:onClick属性查看ID为'button1'的android.support.v7.widget.AppCompatButton –

+0

我正面临同样的问题。如果我只是使用onClick并放置打印语句,那么它每次都能正常工作。在网络上的东西,它给错误 –

+0

这对我来说工作得很好。第一个错误是因为你在主线程中调用geipify()方法而来的。有了上述更改,首先应该解决错误。这个新的错误来自您的layout.xml文件中的方法名称。我已经更新了我的回答 – nitinsh99