Android JSON HttpClient to send data to PHP server with HttpResponse
After lots of reading and searching I have found the problem to be with, I beleive magic_quotes_gpc being enabled on the server.
Thus, using:
json_decode(stripslashes($_POST['vehicle']));
In my example above removes the slashes and allows the JSON to be decoded properly.
Still not sure why sending a StringEntity causes a 403 error?
How can i send JsonObject to server using Https Post request
HttpClient httpclient =new DefaultHttpClient();
HttpPost httppost=new HttpPost(name of the website);
try{
JSONObject j = new JSONObject();
j.put("engineer", "me");
httppost.setEntity(new UrlEncodedFormEntity(j));
HttpResponse response = httpclient.execute(httppost);
/*Checking response*/
if(response!=null)
{
responseBody = EntityUtils.toString(response.getEntity());
}
if(responseBody.equals("ok"))
{
//...do something
}
} catch(ClientProtocolException e) {
} catch (IOException e) {
// TODO Auto-generated catch block
} catch(Exception e){
e.printStackTrace();
}
You can also can take a look in here: Android JSON HttpClient to send data to PHP server with HttpResponse
sending JSON to server using POST in android app
Use this method to make POST Request :
public String makePOSTRequest(String url, List<NameValuePair> nameValuePairs) {
String response = "";
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d(LOGTAG, "POST Response >>> " + response);
return response;
}
Usage :
In Java :
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("json",jsonObject.toString()));
String response = makePOSTRequest(String url, nameValuePairs );
Server Side Php :
$jsonInput = $_POST['json'];
json_decode($jsonInput);
Related Topics
Php: Create an Array for a Range
How to Continue a Session from One Page to Another with PHP
How to Decode Eval( Gzinflate( Base64_Decode(
Max File Number Can PHP Upload at Same Time
Routing Controllers in Subfolders Using Codeigniter
How to Fix the "Base Table or View Not Found: 1146" Error When Running 'PHP Artisan Migrate' Command
Intermittent PHP Abstract Class Error
How to Use Include Within a Function
Why Doesn't Var_Dump Work with Domdocument Objects, While Print($Dom->Savehtml()) Does
PHP Header Redirect in a New Window
Variable Position in Comparision in PHP
Cakephp-3.X: How to Change the Data Type of a Selected Alias
How to Add Some Textfield in Form Register (Laravel Generator Infyom)
Pass Arraylist Bean from Android to Webservice PHP
Decode a Quoted Printable Message in PHP
Xdebug V3 Doesn't Stop Breakpoints in VScode
Simplexml How to Prepend a Child in a Node
Codeigniter Default Controller in a Sub Directory Not Working