2015-04-07 48 views
0

我用WebView加載URL後,我可以插入表單頁面中的一些數據在汽車?加載URL後用WebView將數據插入表單

我試着用:

@Override 
      public void onPageFinished(WebView view, String url) { 
       String user="pippo"; 
       String pwd="secret"; 
       view.loadUrl("javascript:document.getElementById('j_username').value = '"+user+"';document.getElementById('j_password').value='"+pwd+"';"); 
      } 

但不會工作。

的HTML源代碼是:

<html> 

.... 
    <form action="/.../" method="post"> 

       <table align="center"> 
       <tr> 
       <td> 
       <table> 
       <tr> 
       <td>User:</td> 
       <td><input name="j_username" type="text" tabindex="1"></td> 
       </tr> 
       <tr> 
       <td>Password:</td> 
       <td><input name="j_password" type="password" tabindex="2"></td> 
       </tr> 
       <tr> 
       <td colspan="2"><input type="submit" value="Login" tabindex="3"></td> 
       </tr> 
       </table> 
       </td> 
       </tr> 
       </table> 
         <!--</form>--> 
         </form> 

... 
<\html> 
+0

你是哪裏的兩種形式指定ID爲j_username?我只看到名稱,類型和tabindex屬性.. –

+0

ops yes..i必須使用document.getElementByName? –

+0

getElementByName()不存在,您必須使用getElementsByName,它將返回一個集合,並且您必須獲取用於用戶名的位置[0]和用於密碼的位置[1]中的元素。只有這兩個名稱屬性是您加載的html頁面中的前兩個時,這纔會起作用。我強烈建議你使用id屬性,如果你可以更改html,它們更精確,並且如果添加html代碼將不會改變.. –

回答

0

首先讓您的數據到所有Strings,並將其轉發給所在班級你想這樣:

class MyJavaScriptInterface { 

    private Context ctx; 

    MyJavaScriptInterface(Context ctx) { 
     this.ctx = ctx; 
    } 
@JavascriptInterface 
    public void showHTML(String html) throws JSONException { 
     System.out.println(html); 
     JSONObject jsonResponse = new JSONObject(html); 
     String sts = jsonResponse.getString("status"); 
     if (sts.equals("success")) { 
      message = jsonResponse.getString("msg"); 
      approved_status = jsonResponse.getString("approved"); 
      jsonObj = jsonResponse.getJSONObject("data"); 
      user_email = jsonObj.getString("user_email"); 
      phone_no = jsonObj.getString("phone_no"); 
      user_gender = jsonObj.getString("gender"); 
      first_name = jsonObj.getString("first_name"); 
      last_name = jsonObj.getString("last_name"); 
      ids = jsonObj.getString("id"); 
      id = Integer.parseInt(ids); 
} 
} 

如果你的最後一頁是HTML然後使用此代碼:

@Override 
     public void onPageFinished(WebView view, String url) { 

      wb.loadUrl("javascript:window.HtmlViewer.showHTML" 
        + "(document.getElementsByTagName('pre')[0].innerHTML);"); 
     } 
+0

但是我的例子嗎? –

+0

是從哪裏獲取數據的HTML頁面嗎? –

+0

是的,我將代碼發佈到我的答案中 –