【发布时间】:2014-03-21 19:17:43
【问题描述】:
在我的活动中,我正在调用一个函数:
JSONObject jObj = SupportFunctions.returnJSONObject(url);
函数如下所示:
public static JSONObject returnJSONObject(String url) {
JSONObject jObj = null;
InputStream iS = null;
String result = null;
// HTTP
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
iS = entity.getContent();
}
catch (Exception e) {
Log.e("QUIZ", e.getMessage());
return null;
}
return jObj;
}
调用结果:
Pick [Android Application] <terminated>Pick [Android Application]
<disconnected>DalvikVM [localhost:8600]
Pick [Android Application]
DalvikVM [localhost:8600] (may be out of synch)
Thread [<1> main] (Suspended (exception RuntimeException))
<VM does not provide monitor information>
ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2295
ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2349
ActivityThread.access$700(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 159
ActivityThread$H.handleMessage(Message) line: 1316
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 137
ActivityThread.main(String[]) line: 5419
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 525
ZygoteInit$MethodAndArgsCaller.run() line: 1187
ZygoteInit.main(String[]) line: 1003
NativeStart.main(String[]) line: not available [native method]
Thread [<11> Binder_3] (Running) (may be out of synch)
Thread [<10> Binder_2] (Running) (may be out of synch)
Thread [<9> Binder_1] (Running) (may be out of synch)
为什么调用会产生这个错误? HttpResponse response = httpclient.execute(httppost); 似乎是失败的那一行!
【问题讨论】:
-
发布您的
LogCat错误。 -
没有 logcat 错误
-
你返回
jObjwuthout 分配任何值,它是null,这没有任何意义 -
与stackoverflow.com/questions/22489054/… 一样,与通过调试器堆栈进行排序相比,确定是否让应用程序崩溃并从 logcat 中提取异常更容易。完整的跟踪包括各种“由”部分,显示异常消息,您可能会在日志文件中的异常上方找到一些有用的消息。此外,这不是“Dalvik 错误”,它显然是由框架抛出的
RuntimeException。 -
按照设计,Android 应用程序将在主线程中的任何 http 请求中崩溃。您应该能够在 logcat 中看到确切的异常。