Android™
Android™
Load JSON Object from HTTP Response Body
Demonstrates how to load the HTTP response body directly into a JSON object.Chilkat Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean success = false;
CkHttp http = new CkHttp();
CkHttpResponse resp = new CkHttpResponse();
success = http.HttpStr("GET","https://www.chilkatsoft.com/exampledata/sample.json","","","",resp);
if (success == false) {
Log.i(TAG, http.lastErrorText());
return;
}
// A JSON object is JSON that begins with "{" and ends with "}"
CkJsonObject json = new CkJsonObject();
// If we wish to transfer (instead of copy) the JSON from the HttpResponse to the JsonObject, then add the keyword "TakeResponseBody" to UncommonOptions
// This could save memory for extremely large JSON responses.
resp.put_UncommonOptions("TakeResponseBody");
resp.GetBodyJson(json);
json.put_EmitCompact(false);
Log.i(TAG, json.emit());
// Note: If UncommonOptions contained "TakeResponseBody", then the response BodyStr will now be empty:
Log.i(TAG, "----");
Log.i(TAG, resp.bodyStr());
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}