Sample code for 30+ languages & platforms
Android™

Load XML Object from HTTP Response Body

Demonstrates how to load the HTTP response body directly into an XML object.

Chilkat Android™ Downloads

Android™
// 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/inventory.xml","","","",resp);
    if (success == false) {
        Log.i(TAG, http.lastErrorText());
        return;
        }

    CkXml xml = new CkXml();

    //  If we wish to transfer (instead of copy) the XML from the HttpResponse to the Xml, then add the keyword "TakeResponseBody" to UncommonOptions
    //  This could save memory for extremely large XML responses.
    resp.put_UncommonOptions("TakeResponseBody");

    resp.GetBodyXml(xml);

    Log.i(TAG, xml.getXml());

    //  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."
  }
}