Android JSON Httpclient to Send Data to PHP Server with Httpresponse

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



Leave a reply



Submit