HTTP POST 响应到 Android 中的 WebView

发布于 2025-01-07 08:41:26 字数 4786 浏览 0 评论 0原文

我正在尝试使用 HTTP 连接到该页面 邮政。我做了一个 http post 来创建 一个网络视图。我需要从网络视图重定向到另一个页面。但是,当单击“继续”按钮时,会引发异常。

我的代码是

public class ZHttpPostProjActivity extends Activity {
    /** Called when the activity is first created. */

    private WebView mWebView;
    private ProgressDialog progressBar;
    private static final String TAG = "ZHttpPostProjActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.web_view);

    mWebView = (WebView) findViewById(R.id.webview);
    WebSettings webSettings = mWebView.getSettings();

    webSettings.setJavaScriptEnabled(true);
    webSettings.setDomStorageEnabled(true);

    progressBar = ProgressDialog.show(ZHttpPostProjActivity.this, "",
        "Loading...");

    postData();
    }


    private final String URL_REGISTER = "https://www.paypal.com/checkout";

    public void postData() {

    BufferedReader bufferedReader = null;

    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("username", "username"));
        nameValuePairs.add(new BasicNameValuePair("password", "password"));

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(URL_REGISTER);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,
            HTTP.UTF_8));

        HttpResponse response = httpclient.execute(httppost);

        bufferedReader = new BufferedReader(new InputStreamReader(response
            .getEntity().getContent()));
        StringBuffer stringBuffer = new StringBuffer("");
        String line = "";
        String LineSeparator = System.getProperty("line.separator");
        while ((line = bufferedReader.readLine()) != null) {
        stringBuffer.append(line + LineSeparator);
        }
        bufferedReader.close();

        String webData = stringBuffer.toString();
        Log.i(TAG + "web data : ", webData);

        // String webData = new
        // BasicResponseHandler().handleResponse(response);

        Log.i(TAG, "Httppost.getURI().toString(): "
            + httppost.getURI().toString());

        mWebView.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.i(TAG, "Processing webview url click...");
            view.loadUrl(url);
            return true;
        }

        public void onPageFinished(WebView view, String url) {

            Log.i(TAG, "Finished loading URL: " + url);
            if (progressBar.isShowing()) {
            progressBar.dismiss();
            }
        }

        public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl) {
            Log.e(TAG, "Error: " + description + " \n errorCode: "
                + errorCode + "\n  failingUrl: " + failingUrl);

        }
        });

        // mWebView.loadUrl(httppost.getURI().toString());
        mWebView.loadData(webData, "text/html", "UTF-8");
        mWebView.loadDataWithBaseURL(httppost.getURI().toString(), webData,
            "text/html", HTTP.UTF_8, null);

    } catch (Exception ex) {
        ex.printStackTrace();
    }

    }

当单击网络视图中的继续按钮时抛出异常

02-21 11:42:38.539: E/webviewdatabase(2848): Failed in setFormData
02-21 11:42:38.539: E/webviewdatabase(2848): java.net.MalformedURLException: Unknown protocol: about
02-21 11:42:38.539: E/webviewdatabase(2848):    at java.net.URL.<init>(URL.java:288)
02-21 11:42:38.539: E/webviewdatabase(2848):    at java.net.URL.<init>(URL.java:157)
02-21 11:42:38.539: E/webviewdatabase(2848):    at android.webkit.WebViewDatabase.setFormData(WebViewDatabase.java:1032)
02-21 11:42:38.539: E/webviewdatabase(2848):    at android.webkit.BrowserFrame.loadStarted(BrowserFrame.java:384)
02-21 11:42:38.539: E/webviewdatabase(2848):    at android.webkit.JWebCoreJavaBridge.sharedTimerFired(Native Method)
02-21 11:42:38.539: E/webviewdatabase(2848):    at android.webkit.JWebCoreJavaBridge.fireSharedTimer(JWebCoreJavaBridge.java:91)
02-21 11:42:38.539: E/webviewdatabase(2848):    at android.webkit.JWebCoreJavaBridge.handleMessage(JWebCoreJavaBridge.java:108)
02-21 11:42:38.539: E/webviewdatabase(2848):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 11:42:38.539: E/webviewdatabase(2848):    at android.os.Looper.loop(Looper.java:123)
02-21 11:42:38.539: E/webviewdatabase(2848):    at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:673)
02-21 11:42:38.539: E/webviewdatabase(2848):    at java.lang.Thread.run(Thread.java:1019)
02-21 11:42:41.324: E/cache(2848): illegal expires: Sat, Jan 01 2000 01:01:01 GMT

I'm attempting to connect to the page using an HTTP
Post. I do an http post for creating
a webview. i need to redirect to another page from the webview. But when the continue button is clicked exception is thrown .

My code is

public class ZHttpPostProjActivity extends Activity {
    /** Called when the activity is first created. */

    private WebView mWebView;
    private ProgressDialog progressBar;
    private static final String TAG = "ZHttpPostProjActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.web_view);

    mWebView = (WebView) findViewById(R.id.webview);
    WebSettings webSettings = mWebView.getSettings();

    webSettings.setJavaScriptEnabled(true);
    webSettings.setDomStorageEnabled(true);

    progressBar = ProgressDialog.show(ZHttpPostProjActivity.this, "",
        "Loading...");

    postData();
    }


    private final String URL_REGISTER = "https://www.paypal.com/checkout";

    public void postData() {

    BufferedReader bufferedReader = null;

    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("username", "username"));
        nameValuePairs.add(new BasicNameValuePair("password", "password"));

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(URL_REGISTER);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,
            HTTP.UTF_8));

        HttpResponse response = httpclient.execute(httppost);

        bufferedReader = new BufferedReader(new InputStreamReader(response
            .getEntity().getContent()));
        StringBuffer stringBuffer = new StringBuffer("");
        String line = "";
        String LineSeparator = System.getProperty("line.separator");
        while ((line = bufferedReader.readLine()) != null) {
        stringBuffer.append(line + LineSeparator);
        }
        bufferedReader.close();

        String webData = stringBuffer.toString();
        Log.i(TAG + "web data : ", webData);

        // String webData = new
        // BasicResponseHandler().handleResponse(response);

        Log.i(TAG, "Httppost.getURI().toString(): "
            + httppost.getURI().toString());

        mWebView.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.i(TAG, "Processing webview url click...");
            view.loadUrl(url);
            return true;
        }

        public void onPageFinished(WebView view, String url) {

            Log.i(TAG, "Finished loading URL: " + url);
            if (progressBar.isShowing()) {
            progressBar.dismiss();
            }
        }

        public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl) {
            Log.e(TAG, "Error: " + description + " \n errorCode: "
                + errorCode + "\n  failingUrl: " + failingUrl);

        }
        });

        // mWebView.loadUrl(httppost.getURI().toString());
        mWebView.loadData(webData, "text/html", "UTF-8");
        mWebView.loadDataWithBaseURL(httppost.getURI().toString(), webData,
            "text/html", HTTP.UTF_8, null);

    } catch (Exception ex) {
        ex.printStackTrace();
    }

    }

While Clicking the continue button in the webview an exception is thrown

02-21 11:42:38.539: E/webviewdatabase(2848): Failed in setFormData
02-21 11:42:38.539: E/webviewdatabase(2848): java.net.MalformedURLException: Unknown protocol: about
02-21 11:42:38.539: E/webviewdatabase(2848):    at java.net.URL.<init>(URL.java:288)
02-21 11:42:38.539: E/webviewdatabase(2848):    at java.net.URL.<init>(URL.java:157)
02-21 11:42:38.539: E/webviewdatabase(2848):    at android.webkit.WebViewDatabase.setFormData(WebViewDatabase.java:1032)
02-21 11:42:38.539: E/webviewdatabase(2848):    at android.webkit.BrowserFrame.loadStarted(BrowserFrame.java:384)
02-21 11:42:38.539: E/webviewdatabase(2848):    at android.webkit.JWebCoreJavaBridge.sharedTimerFired(Native Method)
02-21 11:42:38.539: E/webviewdatabase(2848):    at android.webkit.JWebCoreJavaBridge.fireSharedTimer(JWebCoreJavaBridge.java:91)
02-21 11:42:38.539: E/webviewdatabase(2848):    at android.webkit.JWebCoreJavaBridge.handleMessage(JWebCoreJavaBridge.java:108)
02-21 11:42:38.539: E/webviewdatabase(2848):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 11:42:38.539: E/webviewdatabase(2848):    at android.os.Looper.loop(Looper.java:123)
02-21 11:42:38.539: E/webviewdatabase(2848):    at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:673)
02-21 11:42:38.539: E/webviewdatabase(2848):    at java.lang.Thread.run(Thread.java:1019)
02-21 11:42:41.324: E/cache(2848): illegal expires: Sat, Jan 01 2000 01:01:01 GMT

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生来就爱笑 2025-01-14 08:41:26

像这样使用

WebView webview = new WebView(this);
setContentView(webview);
byte[] post = EncodingUtils.getBytes("postvariable=value&nextvar=value2", "BASE64");
webview.postUrl("http://www.geenie.nl/AnHeli/mobile/ranking/demo/index.php", post);

Use like this

WebView webview = new WebView(this);
setContentView(webview);
byte[] post = EncodingUtils.getBytes("postvariable=value&nextvar=value2", "BASE64");
webview.postUrl("http://www.geenie.nl/AnHeli/mobile/ranking/demo/index.php", post);
客…行舟 2025-01-14 08:41:26

我不确定这是否是您失败的地方,或者这是否会导致错误,但由于您正在执行 POST,请删除“?”在 URL_REGISTER 变量的末尾。

private final String URL_REGISTER = "https://www.paypal.com/checkout?";

“?”仅在执行 GET 请求时才需要。

大卫

I'm not sure if this is where your failing, or if this will cause an error, but since you're doing a POST, remove the "?" at the end of your URL_REGISTER variable.

private final String URL_REGISTER = "https://www.paypal.com/checkout?";

"?" is only needed when doing a GET request.

David

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文