java - How to properly perform HttpPost on onPageFinished in Android -


in app have webview. if webview loads, want perform httppost fetch variable script. keep getting exception errors, tell me need perform httppost in asynctask. don't know how this, since i'm enough in android developing yet.

this httppost wrote:

httpclient httpclient = new defaulthttpclient(); httppost httppost = new;    httppost("myscript.php");      try {        // add data        list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2);        namevaluepairs.add(new basicnamevaluepair("website", "google.com"));        httppost.setentity(new urlencodedformentity(namevaluepairs));         // execute http post request        httpresponse response = httpclient.execute(httppost);         // writing response log        log.d("http response:", response.tostring());       } catch (clientprotocolexception e) {        // todo auto-generated catch block      } catch (ioexception e) {        // todo auto-generated catch block      } 

this onpagefinished method:

mywebview.setwebviewclient(new webviewclient() {      @override     public void onpagefinished(webview mywebview, string url)     {          mywebview.loadurl("javascript:(function() { " +         "var tetsttest = document.getelementbyid('menu-toggle'); tetsttest.style.display = 'none'; tetsttest.style.display = 'none'; tetsttest.style.display = 'none';" +         "})()");          // when page has finished loading dismiss progress dialog         if (progressdialog != null && progressdialog.isshowing())         {             progressdialog.dismiss();         }      } });  

and php file:

<?php          $website = $_post['website'];      $conn = mysql_connect("localhost", "username", "password") or die("err");     $db = mysql_select_db('database') or die("err");      $sql = "select color colors website='$website'";     $result = mysql_query($sql) or die(mysql_error());     $row = mysql_fetch_array($result);     $color = $row['color'];      print "$color";  ?> 

well network operations in android requires performed in separate thread , asynctask class allows perform threading in elegant manner(painless threading). heres example of asynctask:

public class sendrequestasynctask extends asynctask<void, void, void> {      @override     protected void onpreexecute() {         // todo auto-generated method stub         super.onpreexecute();         //runs in ui thread      }      @override     protected void doinbackground(void... params) {         // todo auto-generated method stub         //perform network operations here background thread         return null;     }      @override     protected void onpostexecute(void result) {         // todo auto-generated method stub         super.onpostexecute(result);         //runs in ui thread can update layout here     } } 

on onpageloading finished method can call asynctask this:

new sendrequestasynctask().execute(); 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -