Android™
Android™
Load JSON Array from HTTP Response Body
Demonstrates how to load the HTTP response body directly into a JsonArray.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/sampleArray.json","","","",resp);
if (success == false) {
Log.i(TAG, http.lastErrorText());
return;
}
// A JSON array is JSON that begins with "[" and ends with "]"
CkJsonArray jarr = new CkJsonArray();
// If we wish to transfer (instead of copy) the JSON from the HttpResponse to the JsonArray, then add the keyword "TakeResponseBody" to UncommonOptions
// This could save memory for extremely large JSON responses.
resp.put_UncommonOptions("TakeResponseBody");
resp.GetBodyJarr(jarr);
Log.i(TAG, jarr.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."
}
}