This question already has answers here:
Error java.lang.RuntimeException: Stub! in Android with Fitnesse testing

(4个答案)


2年前关闭。




因此,由于要访问iTunes查找api,因此我试图从此字符串创建JSON对象,但似乎无法创建JSONObject。我看过每个基于android / java的解决方案,但似乎无法正常工作。谢谢!

import org.json.JSONException;
import org.json.JSONObject;

public class api_caller {

static String api_out = "{\"wrapperType\":\"track\", \"kind\":\"podcast\", \"collectionId\":274450056, \"trackId\":274450056, \"artistName\":\"Giant Bomb\"}";

static String fin = api_out.replace("\\", "");

public static void main(String[] args) {
    System.out.println(fin);
    try {
        JSONObject jsondata = new JSONObject(fin);
    }catch(JSONException e){
        e.printStackTrace();
    }
}

}


编辑:

所以我尝试了Darshan提到的内容。但是我一直都在遇到这个错误。

/Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home/bin/java -Didea.launcher.port=7540 "-Didea.launcher.bin.path=/Applications/Android Studio.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Users/jsho/Library/Android/sdk/platforms/android-23/android.jar:/Users/jsho/Library/Android/sdk/platforms/android-23/data/res:/Users/jsho/projects/simple_podcasts/app/build/intermediates/classes/debug:/Users/jsho/projects/simple_podcasts/app/build/intermediates/exploded-aar/com.android.support/animated-vector-drawable/23.2.1/res:/Users/jsho/projects/simple_podcasts/app/build/intermediates/exploded-aar/com.android.support/animated-vector-drawable/23.2.1/jars/classes.jar:/Users/jsho/projects/simple_podcasts/app/build/intermediates/exploded-aar/com.android.support/support-v4/23.2.1/jars/classes.jar:/Users/jsho/projects/simple_podcasts/app/build/intermediates/exploded-aar/com.android.support/support-v4/23.2.1/res:/Users/jsho/projects/simple_podcasts/app/build/intermediates/exploded-aar/com.android.support/support-v4/23.2.1/jars/libs/internal_impl-23.2.1.jar:/Users/jsho/projects/simple_podcasts/app/build/intermediates/exploded-aar/com.android.support/recyclerview-v7/23.2.1/jars/classes.jar:/Users/jsho/projects/simple_podcasts/app/build/intermediates/exploded-aar/com.android.support/recyclerview-v7/23.2.1/res:/Users/jsho/Library/Android/sdk/extras/android/m2repository/com/android/support/support-annotations/23.2.1/support-annotations-23.2.1.jar:/Users/jsho/projects/simple_podcasts/app/build/intermediates/exploded-aar/com.android.support/support-vector-drawable/23.2.1/res:/Users/jsho/projects/simple_podcasts/app/build/intermediates/exploded-aar/com.android.support/support-vector-drawable/23.2.1/jars/classes.jar:/Users/jsho/projects/simple_podcasts/app/build/intermediates/exploded-aar/com.android.support/design/23.2.1/jars/classes.jar:/Users/jsho/projects/simple_podcasts/app/build/intermediates/exploded-aar/com.android.support/design/23.2.1/res:/Users/jsho/projects/simple_podcasts/app/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.2.1/jars/classes.jar:/Users/jsho/projects/simple_podcasts/app/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.2.1/res:/Applications/Android Studio.app/Contents/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain com.example.jsho.simplepod.api_caller
Exception in thread "main" java.lang.RuntimeException: Stub!
    at org.json.JSONObject.<init>(JSONObject.java:7)
{"wrapperType":"track", "kind":"podcast", "collectionId":274450056, "trackId":274450056, "artistName":"Giant Bomb"}
    at com.example.jsho.simplepod.api_caller.main(api_caller.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

Process finished with exit code 1

最佳答案

无需string.replace()操作,引号已被转义。以下应该可以正常工作:

System.out.println(api_out);
try {
    JSONObject jsondata = new JSONObject(api_out);
    System.out.println(jsondata);
} catch (JSONException e) {
    e.printStackTrace();
}

09-27 18:42