0

I have been trying to get my Android App to post data to a PHP file, which then writes that data to a database, however I'm having a bit of trouble with it.

I'm getting this error, however it's not force closing or anything.

Logcat Output:

08-13 20:29:42.859: I/postData(11950): HTTP/1.1 200 OK
08-13 20:29:42.859: E/log_tag(11950): Error in http connectionjava.lang.IllegalStateException: Content has been consumed

Here is the code in question that's doing all my HTTP POST stuff, the android side of things:

SubmitWord task = new SubmitWord(); 
task.execute(new String[] { "http://www.hanged.comli.com/main.php" });

The above code calls this asynchronous task:

          private class SubmitWord extends AsyncTask<String, Void, String> 
    {
        @Override
        protected String doInBackground(String... urls) 
        {
          String response = "";
          try
          {
            URL = urls[0];
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
            nameValuePairs.add(new BasicNameValuePair("victim",myId));
            nameValuePairs.add(new BasicNameValuePair("rival",newname));
            nameValuePairs.add(new BasicNameValuePair("word","HELLOHOMO"));
            nameValuePairs.add(new BasicNameValuePair("won","0"));

              HttpClient httpclient = new DefaultHttpClient();
              HttpPost httppost = new      
              HttpPost(URL);
              httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

              HttpResponse execute = httpclient.execute(httppost);
              HttpEntity entity = execute.getEntity();
              InputStream is = entity.getContent();

             Log.i("postData", execute.getStatusLine().toString());
             //HttpEntity entity = response.getEntity();
              is = entity.getContent();
          }
          catch(Exception e)
          {
                  Log.e("log_tag", "Error in http connection"+e.toString());
          }
                return response;
            } 

        @Override
        protected void onPostExecute(String result) 
        {
          mText.setText("DONE");
        }

      }

Here is the PHP side of things:

<?php

 /// REMOVED DATABASE DETAILS

$connect = mysql_connect("$mysql_host", "$mysql_user", "$mysql_password")or die("cannot   connect");
 mysql_select_db("$mysql_database", $connect)or die("cannot select DB");

session_start();

$victim = $_POST['victim'];
$rival = $_POST['rival'];
$word = $_POST['word'];
$won = $_POST['won'];

mysql_query("INSERT INTO currentgames (victim, rival, wordguess, won) VALUES('$victim',       '$rival', '$word', '$won'))");

I'm fairly sure it's just the Java/Android part that I've gotten wrong, but I can't figure out for the life of me what I'm doing wrong, I have tried various different methods of POSTING data and read a number of tutorials on using HTTPOST. Maybe I'm just not understanding correctly.

1 Answer 1

2

The culprit is you are calling getContent(); twice.

As per javadoc

Returns a content stream of the entity. Repeatable entities are expected to create a new instance of InputStream for each invocation of this method and therefore can be consumed multiple times. Entities that are not repeatable are expected to return the same InputStream instance and therefore may not be consumed more than once.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, this fixes that error, however nothing seems to be writing to my database, is the HTTP Post and such correct?
Did you try printing the values on php side before inserting to DB? Post looks ok on high-level.
The page is definetly taking post values, confirmed via using some echo's on the page. But it just refuses to post to the database, and with no error, here's my database/sql script: mysql_query("INSERT INTO 'a4382284_hangman'.'currentgames' ('uniqueID' ,'victim' ,'rival' ,'wordguess' ,'won') VALUES ('' , '$victim', '$rival', '$word', '$won')"); Seems syntax correct and everything, can you see anything out of the ordinary?
I don't anything about php syntax, but I would trouble shoot like, add echo right after each line and see which line failing. I suspect, two places, one you didn't get database connection (or) two, the single quotes in insert statement (not sure they are valid there or not in php syntax).
Looks like I'll have to fiddle around a bit, I've got a feeling it's just disliking the SQL or like you say the database is not accepting connection. Thanks for all your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.