- Forum posts: 1
Feb 15, 2016, 2:27:15 PM via Website
Feb 15, 2016 2:27:15 PM via Website
Hi Everybody,
I am developing an android app, an ordering app. I want to send the details of items added to cart in a JSON format to a php page so as to process it there and return the result of the processing.
I am using Android studio.
The code is below
protected String doInBackground(String... args) {
String url = "mysite/mypage";
JSONArray cart_items = new DatabaseHelper().getItemsAsJson() ;
JSONArray pdet = new JSONArray();
String JsonDATA ="";
JSONObject jsonobj = new JSONObject();
try {
jsonobj.put("name", "Aneh");
jsonobj.put("daddrs", "my address");
jsonobj.put("dmob", "9990909090");
jsonobj.put("dlocn", "Kochi");
jsonobj.put("dlmark", "Stadium");
jsonobj.put("uid", "313");
} catch (JSONException e) {
e.printStackTrace();
}
pdet.put(jsonobj);
JSONObject JSONData = new JSONObject();
try {
JSONData.put("cart",cart_items);
JSONData.put("pdet",pdet);
} catch (JSONException e) {
e.printStackTrace();
}
// structure of json
// {"pdet":[{"uid":"313","dlocn":"Kochi","dlmark":"Stadium","dmob":"9990909090","daddrs":"my address","name":"Aneh"}],"cart":[{"cr_itemname":"Domex Floor Cleaner 1 ltr","cr_itemrate":"0","cr_qty":"1","cr_offerrate":"0","cr_total":"100","cr_itemid":"1","cr_slno":"1"},{"cr_itemname":"Nestle EveryDay 400 gm","cr_itemrate":"0","cr_qty":"1","cr_offerrate":"0","cr_total":"100","cr_itemid":"1","cr_slno":"1"},{"cr_itemname":"Colgate Max Fresh Blue Gel 80 gm","cr_itemrate":"0","cr_qty":"1","cr_offerrate":"0","cr_total":"100","cr_itemid":"1","cr_slno":"1"},{"cr_itemname":"Nestle EveryDay","cr_itemrate":"0","cr_qty":"1","cr_offerrate":"0","cr_total":"100","cr_itemid":"1","cr_slno":"1"},{"cr_itemname":"Kelloggs Cornflakes 475 Grams","cr_itemrate":"0","cr_qty":"1","cr_offerrate":"0","cr_total":"100","cr_itemid":"1","cr_slno":"1"},{"cr_itemname":"Ariel 24 Hrs Fresh 500 gm","cr_itemrate":"0","cr_qty":"1","cr_offerrate":"0","cr_total":"100","cr_itemid":"1","cr_slno":"1"},{"cr_itemname":"Colgate Max Fresh Blue Gel 80 gm","cr_itemrate":"0","cr_qty":"1","cr_offerrate":"0","cr_total":"100","cr_itemid":"1","cr_slno":"1"},{"cr_itemname":"Domex Floor Cleaner 1 ltr","cr_itemrate":"0","cr_qty":"1","cr_offerrate":"0","cr_total":"100","cr_itemid":"1","cr_slno":"1"},{"cr_itemname":"Ariel 24 Hrs Fresh 500 gm","cr_itemrate":"0","cr_qty":"1","cr_offerrate":"0","cr_total":"100","cr_itemid":"1","cr_slno":"1"},{"cr_itemname":"Colgate Max Fresh Red Gel 80 gm","cr_itemrate":"0","cr_qty":"1","cr_offerrate":"0","cr_total":"100","cr_itemid":"1","cr_slno":"1"},{"cr_itemname":"Boost 200 gm ","cr_itemrate":"0","cr_qty":"1","cr_offerrate":"0","cr_total":"100","cr_itemid":"1","cr_slno":"1"}]}
// end structure of json
values.put("shpcart", cart_items.toString());
JSONObject json=null;
json= jParser.makeHttpRequest(url, "POST",values);
if (json==null)
{
Log.d("Save order: ", "Null");
}
}
// JSON Parser Class
public class JsonParser {
HttpURLConnection connection;
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
String paramstr;
// constructor
public JsonParser() {
}
public JSONObject makeHttpRequest(String url, String method,
ContentValues params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
URL urlstr = new URL(url);
connection = (HttpURLConnection) urlstr.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
paramstr=params.getAsString("shpcart") ;
DataOutputStream wr;
OutputStream os;
try {
os = connection.getOutputStream();
wr = new DataOutputStream(os);
wr.writeBytes(paramstr);
wr.flush();
wr.close();
is = connection.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
// your logic
// DataOutputStream wr;
// OutputStream os;
// os= connection.getOutputStream();
// wr = new DataOutputStream(os);
// wr.writeBytes(params.toString());
// wr.flush();
// wr.close();
////'''
// OutputStream os;
// os = connection.getOutputStream();
// OutputStreamWriter wr = new OutputStreamWriter(os);
// wr.write(params.toString());
// wr.flush();
//'''''''''''
//BufferedWriter writer;
//OutputStream os;
//os=connection.getOutputStream();
//osw=new OutputStreamWriter();
// writer = new BufferedWriter(new , "UTF-8");
// writer.write( params.toString());
// json data
// writer.close();
//is = connection.getInputStream();
//input stream
}else if(method == "GET"){
URL urlstr = new URL(url);
HttpURLConnection connection = (HttpURLConnection) urlstr.openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.connect();
is = connection.getInputStream();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
e.getCause();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
// php code - saveorderapp.php
$shpcart = $_POST['shpcart'];
$shpcart1 = json_decode($shpcart);
if(!empty($shpcart1))
{
$response["success"] = 1;
echo json_encode($response);
}
else
{
$response["success"] = 0;
echo json_encode($response);
}
As I am new to Android I cannot figure out where is the problem, is it in android or php code. In the php code I want to receive the json as an array so as I can loop through it and insert each item. Please help it is really urgent.
Thanks in advance!!!