2015-04-07 49 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

是的,我将代码发布到我的答案中 –