我正在尝试从我从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数组。
答案 0 :(得分:0)
您收到java.lang.NoClassDefFoundError
因为JVM在运行时找不到org.json.simple.parser.JSONParser
。检查库是否包含在正确的目录中,并添加到类路径中。
答案 1 :(得分:0)
为什么不使用字符串创建一个JSon对象,如下所示:
JSONObject jsonObject = new JSONObject(result);
现在,获得所需的值:
String name = jsonObject.getString("name");