0

我正在尝试从我从 URL 中提取的数据创建的字符串中获取 JSON 数据。到目前为止,这就是我的代码中的内容:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final TextView tv = (TextView)findViewById(R.id.result);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        String username = "username";
        String password = "password";
        String url = "http://www.example.com/api/core/v1/my/";
        String unp = username+":"+password;


        class MyUser {
              public String firstName;
              public String lastName;
        }


        try {
            HttpClient client = new DefaultHttpClient();
            HttpGet header = new HttpGet(url);
            String encoded_login = Base64.encodeToString(unp.getBytes(), Base64.NO_WRAP);
            header.setHeader(new BasicHeader("Authorization", "Basic "+encoded_login));
            HttpResponse response = client.execute(header);
            HttpEntity entity = response.getEntity();

            BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
            for (String line; (line = reader.readLine()) != null;line.replaceAll("throw 'allowIllegalResourceCall is false.';", "")) {
                Vector<Object> vector = new Vector<Object>();
                vector.add(line);
                String result = vector.toString();


                JSONObject json = (JSONObject)new JSONParser().parse(result);
                System.out.println("name=" + json.get("name"));
                System.out.println("width=" + json.get("width"));

                System.out.println(line);
            }       


        } catch (Exception e) {
            e.printStackTrace();
            Log.d(MY_APP_TAG, "Error e: " + e);
        }

        }

这是将正确的 json 数据拉下来,但每次我尝试解析数据时,我都会得到以下信息:

E/AndroidRuntime(923): FATAL EXCEPTION: main
E/AndroidRuntime(923): java.lang.NoClassDefFoundError: org.json.simple.parser.JSONParser
E/AndroidRuntime(923): at basic.authentication.BasicAuthenticationActivity.onCreate(BasicAuthenticationActivity.java:85)
E/AndroidRuntime(923): at android.app.Activity.performCreate(Activity.java:4465)
E/AndroidRuntime(923): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
E/AndroidRuntime(923): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
E/AndroidRuntime(923): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
E/AndroidRuntime(923): at android.app.ActivityThread.access$600(ActivityThread.java:123)
E/AndroidRuntime(923): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
E/AndroidRuntime(923): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(923): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(923): at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime(923): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(923): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(923): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime(923): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime(923): at dalvik.system.NativeStart.main(Native Method)

我是 JSON 新手,因此弄清楚这一点是一个真正的巨大挑战,因此非常感谢任何帮助。

此外,JSON 数据是这样通过的:

{
  "enabled" : true,
  "email" : "example@gmail.com",
  "firstName" : "example firstname",
  "lastName" : "example lastname",
  "name" : "example fullname",
  "level" : {

我知道这些是 JSON 对象而不是 JSON 数组。

4

2 回答 2

0

你得到了一个java.lang.NoClassDefFoundError,因为 JVM 在运行时找不到org.json.simple.parser.JSONParser。检查该库是否包含在正确的目录中并已添加到您的类路径中。

于 2012-07-10T10:33:11.657 回答
0

为什么不使用字符串创建一个 JSON 对象,如下所示:

     JSONObject jsonObject = new JSONObject(result);

现在,像这样获得所需的值:

     String name = jsonObject.getString("name");
于 2012-07-10T10:34:17.693 回答