如何将Android post请求响应传递给javascript函数?

时间:2017-11-12 14:34:50

标签: javascript android android-studio android-webview

我在android studio上创建了一个webview,并希望将web post请求响应放在webiview上的textarea中。 post请求工作正常,我从服务器接收数据,但我在webview中的Javascript函数永远不会被调用来填充textarea。专家是否可以查看我的代码并告诉我如何修复它。提前谢谢。

public class MainActivity extends AppCompatActivity {


    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mWebView= (WebView) findViewById(R.id.activity_main_webview);

        mWebView.loadUrl("file:///android_asset/index.html");
        mWebView.getSettings().setJavaScriptEnabled(true);

        // Force links to open in the WebViewinstead of in a browser
        mWebView.setWebViewClient(new WebViewClient());


    }

    @Override
    protected void onResume()
    {
        super.onResume();

        new PostClass().execute();
    }


    private class PostClass extends AsyncTask<String, Void, Void> {


        @Override
        protected Void doInBackground(String... params) {

            final TextView outputView = (TextView) findViewById(R.id.postOutput);

            try {

                 StrictMode.setThreadPolicy(new Builder().permitAll().build());

                 HttpURLConnection myURLConnection = (HttpURLConnection) new URL("http://myownapi.com/api").openConnection();
                 myURLConnection.setReadTimeout(60000);
                 myURLConnection.setConnectTimeout(60000);
                 myURLConnection.setRequestMethod("POST");
                 myURLConnection.setUseCaches(false);
                 myURLConnection.setDoInput(true);
                 myURLConnection.setDoOutput(true);
                 myURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

                 myURLConnection.setRequestProperty("Content-Language", "en-US");
                 myURLConnection.setRequestProperty("API_KEY", "12345_ABC_MNO_12345678_123ABC");

                 myURLConnection.setRequestProperty("Connection", "Keep-Alive");
                 myURLConnection.addRequestProperty("Content-length", "");



                 OutputStream os = myURLConnection.getOutputStream();

                 os.close();

                 myURLConnection.connect();

                 BufferedReader in = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream()));

                 StringBuffer sb = new StringBuffer();
                 System.out.println(sb);
                 String line;

                 while ((line = in.readLine()) != null) {

                     sb.append(line);
                 }
                 in.close();

                 outputView.setText(sb.toString());
                 //outputView.setText("finished");
                 mWebView.loadUrl("javascript:MyFunction(" + sb.toString() + ")");
                 //mWebView.loadUrl("javascript:MyFunction()");



            } catch (Exception e) {

            }
            return null;
        }
    }
}

webview中的html代码:

<html>

<head>
    <script>
        function MyFunction(myVar)
        {
          //var myVar = 'test data';
          var myTextArea = document.getElementById('myArea');
          myTextArea.innerHTML += myVar;

        };

    </script>
</head>
<body>

<br>
<br>
<textarea id="myArea" rows="30" cols="40"></textarea>
</body>

1 个答案:

答案 0 :(得分:0)

你好,你在

中缺少引号
mWebView.loadUrl("javascript:MyFunction(" + sb.toString() + ")");

现在你所说的就是这个

  

MyFunction的(字符串)

而应该是

  

MyFunction的(&#39;串&#39)

因此,您不是传递字符串,而是尝试像对象一样传递变量。

mWebView.loadUrl("javascript:MyFunction('" + sb.toString() + "')");