HTTP Post funktioniert nicht; leere Variable

G

Garnet

Ambitioniertes Mitglied
1
Ich versuche jetzt schon seit einigen Stunden eine Variable an mein php-Skript zu senden, leider ohne Erfolg. Ich hoffe es kann mir einer dabei helfen.

Mein Java-Code:

Code:
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]final
[/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]ArrayList<NameValuePair> nameValuePairs = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ArrayList<NameValuePair>(1);[/SIZE]
[SIZE=2]nameValuePairs.add([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] BasicNameValuePair([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"param1"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]kommentartext[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]));[/SIZE]

php:

Code:
[SIZE=2]<?php  [/SIZE]
[SIZE=2]$db = new SQLite3('CE2012AppDB2');  [/SIZE]
 
[SIZE=2]if ( false===$db ) {  [/SIZE]
[SIZE=2] exit('Fehler bei sqlite_open');  [/SIZE]
[SIZE=2]}  [/SIZE]
[SIZE=2] $message=$_POST["param1"];[/SIZE]
[SIZE=2]$result = $db->exec('INSERT INTO Kommentare (Kommentartext) VALUES ($message)');  [/SIZE]
 
[SIZE=2]?>[/SIZE]

Die Variable message ist aber leer, obwohl im Java-Code ganz sicher was in der Variable " kommentartext" gespeichert wurde. Woran kann das liegen?

[/LEFT]
 
kontrollier ob du auch wirklich die gleiche Variable nutzt, das herißt -> debug das ganze mal durch und schau was für eine ID die Variable hat wo was drin steht und was für eine ID die Variable hat wo du erwartest das etwas drin steht. Es kann ja sein das er eine neues Objekt etc erzeugt.
 
Ich versteh leider nicht so ganz was du meinst. Wie genau mach ich das?

Könnte der Fehler nicht noch woander liegen?
Hier ist noch mein ganzer Java-Code:

Code:
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public
[/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Kommentarhinzufuegen [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extends[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Activity [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]implements[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] OnClickListener[/SIZE]
[LEFT][SIZE=2]{[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]private[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Button [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]saveButton[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]private[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Button [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]cancelButton[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]private[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] EditText [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]commentEditText[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]private[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] RatingBar [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]ratingBar[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]private[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]static[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]float[/COLOR][/SIZE][/COLOR][/SIZE][/B][I][U][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]rating[/COLOR][/SIZE][/COLOR][/SIZE][/U][/I][SIZE=2];[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]private[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]static[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] String [/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]kommentartext[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2];[/SIZE][/LEFT]
 
[LEFT][SIZE=2][/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onCreate(Bundle savedInstanceState) [/SIZE]
[SIZE=2]{[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].onCreate(savedInstanceState);[/SIZE]
[SIZE=2]setContentView(R.layout.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]kommentar[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/LEFT]
 
[LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]saveButton[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = (Button)findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]btnSave[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]); [/SIZE]
[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]saveButton[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].setOnClickListener([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]);[/SIZE][/LEFT]
 
[LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]cancelButton[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = (Button)findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]btnCancel[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]); [/SIZE]
[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]cancelButton[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].setOnClickListener([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]);[/SIZE][/LEFT]
 
[LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]commentEditText[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = (EditText) findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]txtComments[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]); [/SIZE][/LEFT]
 
[LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]ratingBar[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = (RatingBar) findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]ratingBar[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]); [/SIZE][/LEFT]
 
[LEFT][SIZE=2]}[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onClick(View v) [/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]String [U]message[/U];[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]saveButton[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].isPressed())[/SIZE]
[SIZE=2]{[/SIZE]
[I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]rating[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]ratingBar[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].getRating();[/SIZE]
[I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]kommentartext[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]commentEditText[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].getText().toString();[/SIZE][/LEFT]
 
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]final[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] HttpClient httpclient = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] DefaultHttpClient();[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]final[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] HttpPost httppost = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] HttpPost([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"http://beispiel.de/kommentarephp.php"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]final[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ArrayList<NameValuePair> nameValuePairs = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ArrayList<NameValuePair>(1);[/SIZE]
[SIZE=2]nameValuePairs.add([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] BasicNameValuePair([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"param1"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]kommentartext[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]));[/SIZE]
[SIZE=2]Log.[I]i[/I]([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Test"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]kommentartext[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/LEFT]
 
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] {[/SIZE]
[SIZE=2]httppost.setEntity([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] UrlEncodedFormEntity(nameValuePairs));[/SIZE]
[SIZE=2]} [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (UnsupportedEncodingException e) {[/SIZE]
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// [/COLOR][/SIZE][/COLOR][/SIZE][B][SIZE=2][COLOR=#7f9fbf][SIZE=2][COLOR=#7f9fbf]TODO[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f] Auto-generated catch block[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
[/COLOR][/SIZE][SIZE=2]e.printStackTrace();[/SIZE]
[SIZE=2]}[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] {[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]final[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] HttpResponse [U]response[/U] = httpclient.execute(httppost);[/SIZE]
[SIZE=2]} [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (ClientProtocolException e) {[/SIZE]
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// [/COLOR][/SIZE][/COLOR][/SIZE][B][SIZE=2][COLOR=#7f9fbf][SIZE=2][COLOR=#7f9fbf]TODO[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f] Auto-generated catch block[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
[/COLOR][/SIZE][SIZE=2]e.printStackTrace();[/SIZE]
[SIZE=2]} [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (IOException e) {[/SIZE]
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// [/COLOR][/SIZE][/COLOR][/SIZE][B][SIZE=2][COLOR=#7f9fbf][SIZE=2][COLOR=#7f9fbf]TODO[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f] Auto-generated catch block[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
[/COLOR][/SIZE][SIZE=2]e.printStackTrace();[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]cancelButton[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].isPressed())[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]((EditText)(findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]txtComments[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]))).setText([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]}[/SIZE][/LEFT]
 
 
 
[LEFT][SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]
[/SIZE][/LEFT]

Das Log.i gibt den Text aus der Variablen aus, also kann sie da ja schonmal nicht leer sein.

[/LEFT]
 
EDIT: Ich kenn mich mit php nicht aus.

Falls du eben die antwort gelesen hast, ignoriere sie bitte.

Kontrollier doch mal ob der post befehl richtig abgesetzt wird.
 
  • Danke
Reaktionen: Garnet
Ich hab jetzt mittlerweile den Fehler gefunden, es waren falsche Anführungszeichen im php Skript. @_@ Trotzdem danke für deine Hilfe.
 
  • Danke
Reaktionen: Madlip
Perfekt, wie gesagt ich kenn mich leider mit php nicht aus. Freut mich aber das es nun geht.
 

Ähnliche Themen

SaniMatthias
Antworten
19
Aufrufe
999
swa00
swa00
O
Antworten
15
Aufrufe
3.010
ORHUX
O
K
Antworten
3
Aufrufe
1.177
Kapikalaani
K
Zurück
Oben Unten