1. Download our Official Android App: Forums for Android!

Apps How to get Member Username on Webview (POST Method) ?

Discussion in 'Android Development' started by Vtins, Nov 4, 2017.

  1. Vtins

    Vtins Lurker
    Thread Starter
    Rank:
    None
    Points:
    6
    Posts:
    2
    Joined:
    Nov 4, 2017

    Nov 4, 2017
    2
    2
    6
    Hi AndroidForums im new here with my first question

    Here is my Problem on my Webview. I have created a Webview activity in my android app user Dashboard. Now I need to output the logged-in member username on my php page.

    Here is my Code maybe anyone can help and find my bug I'm using Android Studio

    Android Activity Code:
    Code (Java):
    1.  
    2. public class Activity extends ActionBarActivity {
    3.    private WebView myWebView;
    4.  
    5.    protected void onCreate(Bundle savedInstanceState) {
    6.        super.onCreate(savedInstanceState);
    7.        setContentView(R.layout.activity_cpa);
    8.        myWebView = (WebView)findViewById(R.id.webView);
    9.        WebSettings webSettings = myWebView.getSettings();
    10.        webSettings.setJavaScriptEnabled(true);
    11.        myWebView.loadUrl("http://example.org/page.php?username="+App.getInstance().getUsername());
    12.        myWebView.setWebViewClient(new WebViewClient());
    13.  
    Here is my php Page:

    PHP:
    1. <?php
    2. include_once 'connect_database.php';
    3.  
    4. $username = $_POST['username'];
    5.  
    6. ?>
    7.  
    8. <html>
    9.  
    10.  
    11. <body>
    12. <?php echo $username ?>
    13. </body>
    I dont know why it dosnt work .. maybe anyone can help me

    thanks
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #10
    Points:
    1,988
    Posts:
    8,558
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    8,558
    12,758
    1,988
    Male
    Software developer
    South West of England
    You should examine this line of code:

    Code (Text):
    1.  
    2. myWebView.loadUrl("http://example.org/page.php?username="+App.getInstance().getUsername());
    3.  
    Verify that the call to App.getInstance().getUsername() is returning a non empty String. You can do this by running your app in debug mode, and setting a breakpoint at this line.
     
  3. Vtins

    Vtins Lurker
    Thread Starter
    Rank:
    None
    Points:
    6
    Posts:
    2
    Joined:
    Nov 4, 2017

    Nov 4, 2017
    2
    2
    6
    Hi LV426,
    Thankk you for checking my problem ..
    I found now a solution now and it works great maybe any another user needs this code

    Code (Java):
    1. public class Activity extends ActionBarActivity {
    2.     private WebView myWebView;
    3.  
    4.     protected void onCreate(Bundle savedInstanceState) {
    5.         super.onCreate(savedInstanceState);
    6.         setContentView(R.layout.activity);
    7.         myWebView = (WebView)findViewById(R.id.webView);
    8.         WebSettings webSettings = myWebView.getSettings();
    9.         webSettings.setJavaScriptEnabled(true);
    10.         String url = "http://example.org/page.php";
    11.         String postData = "username="+App.getInstance().getUsername();
    12.  
    13.         myWebView.postUrl(url, EncodingUtils.getBytes(postData, "base64"));
    14.         myWebView.setWebViewClient(new WebViewClient());
    15.  
    16.     }
    17.  
    18. }
    +App.getInstance().getUsername();
    This is my currect string for my loggedin username u need to edit you own POST String
    Now i can get the username via this POST Method on Your Webview page

    PHP:
    1. <?php
    2. include_once 'connect_database.php';
    3. $username = $_POST['username'];
    4. ?>
    5. <html>
    6. <body>
    7. <?php echo $username ?>
    8. </body>
     
    LV426 and Unforgiven like this.

Share This Page

Loading...