我有一个PHP脚本应该从我的数据库返回一个小的ID列表,它在浏览器中工作正常,但是当我尝试在我的应用程序中执行它时,我收到错误。
这是我的代码:
public static String makeCall(String scriptName) {
String address = "http://10.0.2.2/" + scriptName + ".php";
HttpPost httpPost = new HttpPost(address);
HttpClient httpClient = new DefaultHttpClient();
StringBuilder total = new StringBuilder();
try {
//httpPost.setEntity(new UrlEncodedFormEntity(params));
// Execute HTTP Post Request
HttpResponse response = httpClient.execute(httpPost);
InputStream is = response.getEntity().getContent();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line = "";
// Read response until the end
while ((line = rd.readLine()) != null) {
total.append(line);
}
// Return full string
//System.out.println(total.toString());
return total.toString();
} catch (Exception e) {
System.out.println(total == null ? "null" : "notnull");
Log.d("error", e.getMessage());
Log.e("main", "connection error");
}
return "empty";
}
堆栈追踪:
02-24 19:30:08.583: I/System.out(1220): notnull 02-24 19:30:08.594:
D/AndroidRuntime(1220): Shutting down VM 02-24 19:30:08.594:
W/dalvikvm(1220): threadid=1: thread exiting with uncaught exception
(group=0x40a70930)
E/AndroidRuntime(1220): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.phptest/com.example.phptest.MainActivity}:
java.lang.NullPointerException: println needs a message
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException:
println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.d(Log.java:138)
at com.example.phptest.MainActivity.makeCall(MainActivity.java:61)
at com.example.phptest.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
... 11 more
当我删除Log语句或检查它们是否为空时,我收到以下错误:
02-24 20:29:27.523: E/ActivityThread(648): Service
com.android.exchange.ExchangeService has leaked ServiceConnection
com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d0ce98
that was originally bound here
E/ActivityThread(648): android.app.ServiceConnectionLeaked: Service
com.android.exchange.ExchangeService has leaked ServiceConnection
com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d0ce98
that was originally bound here
at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
at android.app.ContextImpl.bindService(ContextImpl.java:1418)
at android.app.ContextImpl.bindService(ContextImpl.java:1407)
at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
答案 0 :(得分:1)
更改
Log.d("error", e.getMessage());
到
if (e.getMessage() != null) {
Log.d("error", e.getMessage());
}