Back to Collection Items
// 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);
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkHttp http = new CkHttp();
boolean success;
// Use this online tool to generate code from sample JSON: Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "address": "<string>",
// "item": {
// "itemId": "<integer>",
// "name": "<string>",
// "price": "<integer>",
// "description": "<string>"
// }
// }
CkJsonObject json = new CkJsonObject();
json.UpdateString("address","<string>");
json.UpdateString("item.itemId","<integer>");
json.UpdateString("item.name","<string>");
json.UpdateString("item.price","<integer>");
json.UpdateString("item.description","<string>");
http.SetRequestHeader("Content-Type","application/json");
CkHttpResponse resp = http.PostJson3("https://api.simplekart.com/orders","application/json",json);
if (http.get_LastMethodSuccess() == false) {
Log.i(TAG, http.lastErrorText());
return;
}
CkStringBuilder sbResponseBody = new CkStringBuilder();
resp.GetBodySb(sbResponseBody);
CkJsonObject jResp = new CkJsonObject();
jResp.LoadSb(sbResponseBody);
jResp.put_EmitCompact(false);
Log.i(TAG, "Response Body:");
Log.i(TAG, jResp.emit());
int respStatusCode = resp.get_StatusCode();
Log.i(TAG, "Response Status Code = " + String.valueOf(respStatusCode));
if (respStatusCode >= 400) {
Log.i(TAG, "Response Header:");
Log.i(TAG, resp.header());
Log.i(TAG, "Failed.");
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "orderId": 674,
// "items": [
// {
// "itemId": 1657,
// "name": "Smart WiFi Router",
// "price": 2096,
// "description": "Wifi router/wireless router. One of routers for wireless internet, wireless routers for home. Dual band router and gigabit router. Ideal as internet router also gaming router. Long range coverage with high speed. Compatible with all 802.11ac devices and below. 5ghz router/5g router Frequency Range: 2.4GHz and 5GHz; Interface Available: 4 x 10/100/1000Mbps LAN Ports, 1 10/100/1000Mbps WAN Port, 1 USB 2.0 Ports; Protocols Supported: Supports IPv4 and IPv6; System Requirement: Microsoft Windows 98SE NT 2000 XP Vista, or Windows 7 Windows 8/8.1/10, MAC OS NetWare UNIX or Linux; Wireless Standards: IEEE 802.11ac/n/a 5GHz, IEEE 802.11b/g/n 2.4GH"
// }
// ],
// "arrivalDate": "2020-3-04T13:07:45.962Z",
// "orderDate": "2020-03-06T19:27:26.733Z",
// "orderTotal": 2096,
// "address": "95 Tarkiln Hill St.Laurel, MD 20707"
// }
// Sample code for parsing the JSON response...
// Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code
int itemId;
String name;
int price;
String description;
int orderId = jResp.IntOf("orderId");
String arrivalDate = jResp.stringOf("arrivalDate");
String orderDate = jResp.stringOf("orderDate");
int orderTotal = jResp.IntOf("orderTotal");
String address = jResp.stringOf("address");
int i = 0;
int count_i = jResp.SizeOfArray("items");
while (i < count_i) {
jResp.put_I(i);
itemId = jResp.IntOf("items[i].itemId");
name = jResp.stringOf("items[i].name");
price = jResp.IntOf("items[i].price");
description = jResp.stringOf("items[i].description");
i = i + 1;
}
}
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."
}
}
Curl Command
curl -X POST
-H "Content-Type: application/json"
-d '{
"address": "<string>",
"item": {
"itemId": "<integer>",
"name": "<string>",
"price": "<integer>",
"description": "<string>"
}
}'
https://api.simplekart.com/orders
Postman Collection Item JSON
{
"name": "Post order details to the database",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"address\": \"<string>\",\n \"item\": {\n \"itemId\": \"<integer>\",\n \"name\": \"<string>\",\n \"price\": \"<integer>\",\n \"description\": \"<string>\"\n }\n}"
},
"url": {
"raw": "{{baseUrl}}/orders",
"host": [
"{{baseUrl}}"
],
"path": [
"orders"
]
},
"description": "Post item order to orders database"
},
"response": [
{
"name": "OK - Order created",
"originalRequest": {
"method": "POST",
"header": [
],
"body": {
"mode": "raw",
"raw": "{\n \"items\": [\n {\n \"itemId\": 1657,\n \"name\": \"Smart WiFi Router\",\n \"price\": 2096,\n \"description\": \"Wifi router/wireless router. One of routers for wireless internet, wireless routers for home. Dual band router and gigabit router. Ideal as internet router also gaming router. Long range coverage with high speed. Compatible with all 802.11ac devices and below. 5ghz router/5g router Frequency Range: 2.4GHz and 5GHz; Interface Available: 4 x 10/100/1000Mbps LAN Ports, 1 10/100/1000Mbps WAN Port, 1 USB 2.0 Ports; Protocols Supported: Supports IPv4 and IPv6; System Requirement: Microsoft Windows 98SE NT 2000 XP Vista, or Windows 7 Windows 8/8.1/10, MAC OS NetWare UNIX or Linux; Wireless Standards: IEEE 802.11ac/n/a 5GHz, IEEE 802.11b/g/n 2.4GH\"\n }\n ],\n \"address\": \"95 Tarkiln Hill St.Laurel, MD 20707\"\n}"
},
"url": {
"raw": "{{baseUrl}}/orders",
"host": [
"{{baseUrl}}"
],
"path": [
"orders"
]
}
},
"status": "Created",
"code": 201,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"cookie": [
],
"body": "{\n \"orderId\": 674,\n \"items\": [\n {\n \"itemId\": 1657,\n \"name\": \"Smart WiFi Router\",\n \"price\": 2096,\n \"description\": \"Wifi router/wireless router. One of routers for wireless internet, wireless routers for home. Dual band router and gigabit router. Ideal as internet router also gaming router. Long range coverage with high speed. Compatible with all 802.11ac devices and below. 5ghz router/5g router Frequency Range: 2.4GHz and 5GHz; Interface Available: 4 x 10/100/1000Mbps LAN Ports, 1 10/100/1000Mbps WAN Port, 1 USB 2.0 Ports; Protocols Supported: Supports IPv4 and IPv6; System Requirement: Microsoft Windows 98SE NT 2000 XP Vista, or Windows 7 Windows 8/8.1/10, MAC OS NetWare UNIX or Linux; Wireless Standards: IEEE 802.11ac/n/a 5GHz, IEEE 802.11b/g/n 2.4GH\"\n }\n ],\n \"arrivalDate\": \"2020-3-04T13:07:45.962Z\",\n \"orderDate\": \"2020-03-06T19:27:26.733Z\",\n \"orderTotal\": 2096,\n \"address\": \"95 Tarkiln Hill St.Laurel, MD 20707\"\n}"
}
]
}