简体   繁体   中英

sending data to PHP from android

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);
?>

I think you are setting the json data as a header and then trying to read it as POST data.

This line puts the JSON as a HEADER named "json":

request.setHeader("json", json.toString());

And the the PHP line tries to get the json string as POST data:

$obj=$_POST['json'];

In summary: a header is not the same as a POST field.

What headers do you see from your PHP script?

Take a look at this answer for POSTing data: Sending POST data in Android

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM