android - HTTP post message with Java and PHP -


i new php , java http client, trying post message php script on server, tried following example: http://webtutsdepot.com/2011/11/15/android-tutorial-how-to-post-data-from-an-android-app-to-a-website/

however not able understand how send json result android app, following code using:

java:

      public void send(view v)       {      // message message text box      string msg = et.gettext().tostring();         // make sure fields not empty      if (msg.length()>0)      {         httpclient httpclient = new defaulthttpclient();         httppost httppost = new httppost("http://animsinc.com/query.php");      try {        list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2);        namevaluepairs.add(new basicnamevaluepair("id", "12345"));        namevaluepairs.add(new basicnamevaluepair("message", msg));        httppost.setentity(new urlencodedformentity(namevaluepairs));        httpclient.execute(httppost);        et.settext(""); // clear text box      } catch (clientprotocolexception e) {          // todo auto-generated catch block      } catch (ioexception e) {          // todo auto-generated catch block      }       }      else      {         // display message if text fields empty          toast.maketext(getbasecontext(),"all field required",toast.length_short).show();      }   } 

php:

       <?php         require 'phtry.php';         $message = $_post["message"];          $query = "select `surname`,`firstname` `users`";          $query1 = "select *  `users` id = $message";        if ($query_run = mysql_query($query1)){  //echo 'success.';  while ($query_row = mysql_fetch_assoc($query_run)){  $surname = $query_row['surname']; $firstname = $query_row['firstname'];    }   $out [] = $query_row; print(json_encode($out)); // if check web browser [false] display here  }else{     echo 'no success'; }       ?> 

as new this, want know if doing things correct.

your code mess on php side. can't echo things. cause json parsing fail.

i recommend use lightweight gson library google self. use pojo classes parse json. once have pojo class gson, have use httpresponse httppost read response itself, this:

        httpclient client = new defaulthttpclient();         httppost post = new httppost(url);          try {             list<namevaluepair> values = new arraylist<namevaluepair>(2);             values.add(new basicnamevaluepair("id", "12345"));             values.add(new basicnamevaluepair("message", "asdf");             post.setentity(new urlencodedformentity(values));              httpresponse httpresponse = client.execute(post);             httpentity entity = httpresponse.getentity();             inputstream stream = entity.getcontent();              gson gson = new gson();             reader reader = new inputstreamreader(stream);              response finishresponse = gson.fromjson(reader, response.class);             return finishresponse;         } catch (exception e) {             e.printstacktrace();         }         return null; 

in case, pojo class response class created.


Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -