senden von JSON auf server mit Hilfe der POST im android-app
Ich bin momentan dabei, eine android app zu senden, JSON eine serverseitige php-Skript und speichern Sie in einer Datenbank
Hier ist der entsprechende code aus der app
public String POST(){
String url = "http://192.168.150.1/t2.php";
InputStream inputStream = null;
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
String json = "";
JSONObject jsonObject = new JSONObject();
jsonObject.put("str", "bang");
jsonObject.put("lat", 3.10);
jsonObject.put("lon", 3.10);
json = jsonObject.toString();
Toast.makeText(this, json, Toast.LENGTH_LONG).show();
StringEntity se = new StringEntity(json);
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse httpResponse = httpclient.execute(httpPost);
inputStream = httpResponse.getEntity().getContent();
if(inputStream != null)
result = "success";
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
return result;
}
und hier ist das php-Skript
<?php
$connection = mysql_connect("192.168.150.1","****","****");
if (!$connection) {
die("Database connection failed: " . mysql_error());
}
echo "connection success\n";
$db_select = mysql_select_db("ps1",$connection);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
echo "db selections success";
?>
<html>
<head>
<title> hl</title>
</head>
<body>
<p>hello</p>
<?php
$js = json_decode(file_get_contents('php://input'));
//$js = json_decode($_POST); ------------ ******
$atr ='';
$val = '';
foreach ($js as $a => $v)
{
$atr = $atr.','.$a;
$val = $val.','.$v;
}
$atr = ltrim($atr,',');
$val = ltrim($val,',');
echo "insert into js (".$atr.") values (" .$val . ");";
$result = mysql_query("insert into js (".$atr.") values (" .$val . ");", $connection);
if (!$result) {
die("Database query failed: " . mysql_error());
}
?>
</body>
</html>
<?php
mysql_close($connection);
?>
Nun, wenn Sie versuchen, senden Sie ein JSON-Objekt aus der app passiert nichts
aber wenn ich versuche
curl -H "Content-Type: application/json" -d '{"str":"\"hello\"","lat":"3.1","lon":"5.2"}' localhost/t2.php
aus der Kommandozeile funktioniert es.
Auch wenn ich versuche, heben Sie die Auskommentierung der $js = json_decode($_POST); Zeile in das PHP-Skript und kommentieren Sie den php://input line, dann das JSON-Objekt wird nicht übertragen, aber die Werte,"", 0,0 in die Datenbank eingefügt werden
Ich denke, es muss ein Fehler mit dem app-code.Ich bin nach dem tutorial und den code von hier http://hmkcode.com/android-send-json-data-to-server/
FRAGEN
- Kann mir jemand erklären, was ich falsch mache und eine plausible Lösung?
- was ist der Unterschied zwischen php://input und $_POST?
Dank
QUEST
json_decode($_POST)
als $_POST ist ein array.Danke, ich bin neu in PHP, also wie kann ich das decodieren des JSON-Objekt?
Sie versenden 'Post' Anfrage können Sie abrufen von Daten im post-Anfrage. PHP-CODE wie $_POST...
kannst du ein bisschen genauer sein?
überprüfen Sie dies: stackoverflow.com/questions/8945879/...
InformationsquelleAutor gokul_uf | 2014-06-20
Du musst angemeldet sein, um einen Kommentar abzugeben.
Diese Methode verwenden, um POST-Anforderung :
Verwendung :
In Java :
Server-Seite Php :
InformationsquelleAutor Shivam Verma
Beim senden einer post-Anfrage an den server, Sie werden get-Parameter in der 'Post' sein (wie pro Ihre android-code).
Hier ist die Lösung:
Hoffe, obige code sollte sinnvoll sein.
InformationsquelleAutor Rajnish