I have a problem sending data to php script from android. I've searched online for the answer why it's not working but none of answers work. I've also tried using the $_ GET and $_SERVER but also it doesn't work. Any suggestions what I'm doing wrong? Android code:
int timout = 10000;
try {
JSONObject json = new JSONObject();
json.put("skola", regid);
Log.w("test","json array== "+json);
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,
timout);
HttpConnectionParams.setSoTimeout(httpParams, timout);
HttpClient client = new DefaultHttpClient(httpParams);
HttpPost request = new HttpPost(url);
request.setEntity(new ByteArrayEntity(json.toString().getBytes(
"UTF8")));
request.setHeader("json", json.toString());
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
String result = convertStreamToString(instream);
Log.i("Read from server", result);
Toast.makeText(SettingsActivity.this, result,
Toast.LENGTH_LONG).show();
}
} catch (Throwable t) {
Toast.makeText(SettingsActivity.this, "Request failed: " + t.toString(),
Toast.LENGTH_LONG).show();
}
and my php code:
<?php
$obj=$_POST['json'];
$db=json_decode($obj,true);
file_put_contents('myTextFile.txt',$db);
$con = mysql_connect("localhost","avg","1zmaInas");
mysql_select_db("datubazes", $con);
mysql_query ("set character_set_client='utf8'");
mysql_query ("set character_set_results='utf8'");
mysql_query ("set collation_connection='utf8_general_ci'");
mysql_query ("SET NAMES utf8");
$result = mysql_query("SELECT * FROM saraksts WHERE skola='$db' ");
while($row = mysql_fetch_assoc($result))
{
$db=$row['datubaze'];
}
$con = mysql_connect("localhost","avg","1zmaInas");
mysql_select_db($db, $con);
$result = mysql_query("SELECT klase FROM klases");
while($row = mysql_fetch_assoc($result))
{
$output[]=$row;
}
print(json_encode($output));
mysql_close($con);
?>