【问题标题】:Function call results in Dalvik error函数调用导致 Dalvik 错误
【发布时间】: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 错误
  • 你返回jObj wuthout 分配任何值,它是null,这没有任何意义
  • stackoverflow.com/questions/22489054/… 一样,与通过调试器堆栈进行排序相比,确定是否让应用程序崩溃并从 logcat 中提取异常更容易。完整的跟踪包括各种“由”部分,显示异常消息,您可能会在日志文件中的异常上方找到一些有用的消息。此外,这不是“Dalvik 错误”,它显然是由框架抛出的 RuntimeException
  • 按照设计,Android 应用程序将在主线程中的任何 http 请求中崩溃。您应该能够在 logcat 中看到确切的异常。

标签: android json http


【解决方案1】:

从你的问题,我可以看到主线程被挂起。假设您在主线程中执行 HTTP 请求,这可能是您的问题。通常它会引发 NetworkOnMainThreadException,我相信当您继续使用调试器时会发生这种情况。

由于您不能在主线程中执行 HTTP 请求,因此解决方案可以是:

JSONArray mJsonArray;

// Inner class to do http request out from main thread
class HttpRequestTask extends AsyncTask<String, Void, Void> {
    @Override
    protected Void doInBackground(String... url) {
        InputStream iS = null;

        // HTTP
        try {

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url[0]);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            iS = entity.getContent();
        }
        catch (Exception e) {
            Log.e("QUIZ", e.getMessage());
            return null;
        }

        if (iS != null) {
            mJsonArray = new JSONArray(urlInputStream.toString());
        }

        return null;
    }

    protected void onPostExecute(Long result) {
        // A toast to inform that the http requeset just finished.
        // The json object will be available in mJsonArray class field.
        Toast.makeText(YourActivity.this, "HTTP request finished", Toast.LENGTH_SHORT).show();
    }
}

最后,创建异步任务并运行它:

new HttpRequestTask().execute(url);

【讨论】:

  • 它不起作用。我的程序不知道 JsonParser。
  • JsonParser 是一个假设类,它将读取您的输入流并将其创建/解析为 json 对象。我现在用真正的方法编辑。
【解决方案2】:

这可能不是问题,但我们永远不知道:您是否在清单中要求互联网许可?

<uses-permission android:name="android.permission.INTERNET" /> 

【讨论】:

    猜你喜欢
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-26
    相关资源
    最近更新 更多