Android™
Android™
POST JSON to REST API with non-us-ascii Chars Escaped
See more REST Examples
Demonstrates how to POST to a REST API with non-usascii chars within JSON Unicode escaped.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;
success = false;
CkRest rest = new CkRest();
// Connect using TLS.
boolean bAutoReconnect = true;
success = rest.Connect("chilkatsoft.com",443,true,bAutoReconnect);
// Load JSON containing the following Korean text.
// {
// "BillAddr": {
// "Id": "239615",
// "Line1": "류리하",
// "Line2": "류리하류리하",
// "City": "류리하류리하",
// "Country": "US",
// "CountrySubDivisionCode": "AK",
// "PostalCode": "류리하"
// }
// }
CkJsonObject json = new CkJsonObject();
json.put_EmitCompact(false);
success = json.LoadFile("qa_data/json/korean.json");
if (success == false) {
Log.i(TAG, json.lastErrorText());
return;
}
success = rest.AddHeader("Content-Type","application/json; charset=UTF-8");
CkStringBuilder sb = new CkStringBuilder();
json.EmitSb(sb);
sb.Encode("unicodeescape","utf-8");
Log.i(TAG, sb.getAsString());
// The StringBuilder contains this:
// {
// "BillAddr": {
// "Id": "239615",
// "Line1": "\ub958\ub9ac\ud558",
// "Line2": "\ub958\ub9ac\ud558\ub958\ub9ac\ud558",
// "City": "\ub958\ub9ac\ud558\ub958\ub9ac\ud558",
// "Country": "US",
// "CountrySubDivisionCode": "AK",
// "PostalCode": "\ub958\ub9ac\ud558"
// }
// }
CkStringBuilder sbResp = new CkStringBuilder();
success = rest.FullRequestSb("POST","/echo_request_body.asp",sb,sbResp);
if (success == false) {
Log.i(TAG, rest.lastErrorText());
return;
}
// Show the response.
Log.i(TAG, "Json Response: " + sbResp.getAsString());
}
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."
}
}