Android : Form data sending from android to PHP JSON server

on Tuesday, April 14, 2015


My server is receiving the data with the following line, managed to receive the data via "postman" google chrome plugin but will return 500 server error while sending from mobile. I not sure what's wrong.



$orders = json_decode(Input::get('orders'));


Mobile JSON Part



JSONObject jsonObject = new JSONObject();
JSONArray jOrderArray = new JSONArray();
JSONObject jOrderObject = new JSONObject();
jOrderObject.put("clubs", Integer.parseInt(clubsEditText.getText().toString()));

JSONArray jItemArray = new JSONArray();
for (Item aItem : items) {
if (aItem.getNumbers() == null || aItem.getNumbers().equals("")) // Skip if no number entered.
continue;
JSONObject jItemObject = new JSONObject();
jItemObject.put("order_id", aItem.getOrderID());
jItemObject.put("optionA", aItem.getOptionA());
jItemObject.put("optionB", aItem.getOptionB());
jItemArray.put(jItemObject);
}
jOrderObject.put("items", jItemArray);
jOrderArray.put(jOrderObject);
jsonObject.put("orders", jOrderObject);

// --- POST API
new PostTask().execute(jOrderObject.toString());


Mobile Form Data Part private class PostTask extends AsyncTask {



@Override
protected String doInBackground(String... json) {
List<NameValuePair> params = new ArrayList<>();
// Create data variable for sent values to server
HttpResponse response = null;
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(API_URI);
try {
// Execute HTTP Post Request
//httppost.setEntity(new StringEntity(json[0], "UTF-8"));
//json[0].toString()
params.add(new BasicNameValuePair("orders", json[0]));
httppost.setEntity(new UrlEncodedFormEntity(params));
httppost.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded");
response = httpclient.execute(httppost);
Log.d("Params", params.toString());
} catch (IOException e) {
Log.e("Error", "IOException: " + e);
}

StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
Log.i("MPT_POST_STATUS", "OK");
}

return response.getStatusLine().toString();
}

@Override
protected void onPostExecute(final String result) {
super.onPostExecute(result);
Toast.makeText(getActivity(), result + ". Please contact developer.", Toast.LENGTH_LONG).show();
Log.i("MPT_POST_STATUS", "Response: " + result);
}
}

0 comments:

Post a Comment