我正在实现从 PHP 获取我的 android 应用程序中我的设备/移动设备的 IP 地址的应用程序。我正在使用 httpGet() 方法来获取 IP 地址。我的 logcat 中有 NullPointerException。
这是我的代码。
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);
}
}
这是我的 LogCat,我得到了 nullPointerException
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)