Note that there are some explanatory texts on larger screens.

plurals
  1. POGet IP Address in Android
    primarykey
    data
    text
    <p>I'm implementing application for getting the IP Address of my device/mobile in my android app from PHP. I'm using httpGet() method for getting the Ip Address. I got NullPointerException in my logcat. </p> <p>Here is my code.</p> <pre><code>public class Code_Demo extends Activity { Button btnSubmitCode; EditText editText_Code; TextView textView_ReadCode; String pref_filename="code_file"; String strReadFile; HttpClient httpclient; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.code); editText_Code=(EditText)findViewById(R.id.edText_Code); textView_ReadCode=(TextView)findViewById(R.id.textView_ReadCode); btnSubmitCode=(Button)findViewById(R.id.button_Submit); btnSubmitCode.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub String strCode = editText_Code.getText().toString().trim(); create_File(strCode); if(editText_Code.getText().toString().isEmpty()) { Toast.makeText(getApplicationContext(),"Please Enter Code", Toast.LENGTH_SHORT).show(); } SharedPreferences sharedPref1 =getSharedPreferences(pref_filename, 0); SharedPreferences.Editor editor = sharedPref1.edit(); editor.putString("code",strCode); Log.e("Editor Num 1:", strCode); editor.commit(); //readFile(); try { getIp_Address(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Intent intent_CodeDemo = new Intent(Code_Demo.this , Gsk_Demo.class); startActivity(intent_CodeDemo); } }); } // onCreate() method end here //Get Ip Address public void getIp_Address() throws IOException { HttpGet httppost1 = new HttpGet("http://api.externalip.net/ip"); HttpResponse response = null; try { response = httpclient.execute(httppost1); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } HttpEntity ht = response.getEntity(); BufferedHttpEntity buf = new BufferedHttpEntity(ht); InputStream is = buf.getContent(); BufferedReader r = new BufferedReader(new InputStreamReader(is)); StringBuilder total = new StringBuilder(); String ipaddress = r.readLine(); Log.e("ipaddress",ipaddress); } } </code></pre> <p>Here Is My LogCat which i got nullPointerException</p> <pre><code>11-13 15:51:04.245: W/KeyCharacterMap(1077): No keyboard for id 0 11-13 15:51:04.245: W/KeyCharacterMap(1077): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 11-13 15:51:05.631: E/Editor Num 1:(1077): gskhmjd 11-13 15:51:05.662: D/AndroidRuntime(1077): Shutting down VM 11-13 15:51:05.662: W/dalvikvm(1077): threadid=1: thread exiting with uncaught exception (group=0x40015560) 11-13 15:51:05.671: E/AndroidRuntime(1077): FATAL EXCEPTION: main 11-13 15:51:05.671: E/AndroidRuntime(1077): java.lang.NullPointerException 11-13 15:51:05.671: E/AndroidRuntime(1077): at com.gsk.Code_Demo.getIp_Address(Code_Demo.java:94) 11-13 15:51:05.671: E/AndroidRuntime(1077): at com.gsk.Code_Demo$1.onClick(Code_Demo.java:71) 11-13 15:51:05.671: E/AndroidRuntime(1077): at android.view.View.performClick(View.java:2485) 11-13 15:51:05.671: E/AndroidRuntime(1077): at android.view.View$PerformClick.run(View.java:9080) 11-13 15:51:05.671: E/AndroidRuntime(1077): at android.os.Handler.handleCallback(Handler.java:587) 11-13 15:51:05.671: E/AndroidRuntime(1077): at android.os.Handler.dispatchMessage(Handler.java:92) 11-13 15:51:05.671: E/AndroidRuntime(1077): at android.os.Looper.loop(Looper.java:123) 11-13 15:51:05.671: E/AndroidRuntime(1077): at android.app.ActivityThread.main(ActivityThread.java:3683) 11-13 15:51:05.671: E/AndroidRuntime(1077): at java.lang.reflect.Method.invokeNative(Native Method) 11-13 15:51:05.671: E/AndroidRuntime(1077): at java.lang.reflect.Method.invoke(Method.java:507) 11-13 15:51:05.671: E/AndroidRuntime(1077): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 11-13 15:51:05.671: E/AndroidRuntime(1077): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 11-13 15:51:05.671: E/AndroidRuntime(1077): at dalvik.system.NativeStart.main(Native Method) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload