Here is my code to send the data from android app to php app which is running in my localhost. It is showing “Connection to http:// localhost refused” . Please help me
public void postData() throws JSONException
{
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost/xampp/FeelSafeSecurity/connectiondemo.php");
JSONObject json = new JSONObject();
try {
// prepare JSON data:
json.put("name", "santhosh");
json.put("age", "24");
JSONArray postjson=new JSONArray();
postjson.put(json);
// Post the data:
httppost.setHeader("json", json.toString());
httppost.getParams().setParameter("jsonpost",postjson);
// Execute HTTP Post Request
System.out.print(json);
HttpResponse response = httpclient.execute(httppost);
// for JSON:
if(response != null)
{
InputStream is = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
text = sb.toString();
}
tv.setText(text);
}
catch (ClientProtocolException e)
{
System.out.println(e.getMessage());
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
And here is my php code to receive the data. I’am new to PHP.
<?php
$json = $_SERVER['HTTP_JSON'];
echo "JSON: \n";
echo "--------------\n";
var_dump($json);
echo "\n\n";
$data = json_decode($json);
echo "Array: \n";
echo "--------------\n";
var_dump($data);
echo "\n\n";
echo "Result: \n";
echo "--------------\n";
echo "\n\nName : ".$data->name."\n\n Age : ".$data->age;
?>
Don’t use localhost use your IP address. Localhost is only for the LOCAL server