I am trying to get JSON data from a string that I have created from data I have pulled down from a URL. So far this is what I have in my code:
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);
}
}
This is pulling the correct json data down, but every time i try to parse the data i am getting the following:
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)
I am new to JSON so figuring this out is proving a real big challenge so any help with this is much appreciated.
Also, the JSON data come through like this:
{
"enabled" : true,
"email" : "[email protected]",
"firstName" : "example firstname",
"lastName" : "example lastname",
"name" : "example fullname",
"level" : {
I know that these are JSON objects and not JSON arrays.
java.lang.NoClassDefFoundError
- i.e. it's not finding your JSON library. Did you download another JSON implementation but haven't put the .jar in a folder namedlibs
in your project? – Jens Jul 10 '12 at 10:35AsyncTask
- and your method of turning the response into a String is at best dysfunctional - you should start by using theorg.apache.http.util.EntityUtils#toString(HttpEntity)
method (also included in the SDK) – Jens Jul 10 '12 at 14:43