无法让Android WebView的postUrl与端口一起使用

时间:2012-10-19 19:33:06

标签: java android webview android-webview

我的WebView工作正常,直到我添加一个端口。此时我没有看到任何类型的错误,我在HTTP流量中看不到任何内容。

private Context vContext;
private String vPackageName;
private String vPlacementID;

static private double TRACK_VERSION = 1.0;
static private String TRACK_HOST = "foobar.com:8007";
static private String TRACK_HANDLER = "/event";

public void reportAppOpen(Context context, String placementID) {
    Log.d("FOO", "Tracking");
    if (context == null) {
        return;
    }
    vContext = context;
    vPackageName = vContext.getPackageName();
    vPlacementID = placementID;

    WebView webview = new WebView(context);
    webview.setVisibility(WebView.GONE);
    webview.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Log.d("FOO", "error code:" + errorCode);
                super.onReceivedError(view, errorCode, description, failingUrl);
        }
     });
    webview.getSettings().setJavaScriptEnabled(true);

    List<NameValuePair> params = trackingParams();
    String paramString = URLEncodedUtils.format(params, "utf-8");
    String url = "http://" + TRACK_HOST + TRACK_HANDLER;
    webview.postUrl(url, EncodingUtils.getBytes(paramString, "base64"));

    Log.d("FOO", "Loading URL");        
}

我知道我的代码中还有其他一些奇怪的东西,但是现在我只想专注于让postUrl工作。

编辑:在logcat中我发现这不确定它是否相关。

10-19 13:44:21.560: D/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol

1 个答案:

答案 0 :(得分:1)

请尝试使用WebChromeClient并覆盖以下方法:

   @Override
public void onConsoleMessage(String message, int lineNumber, String sourceID) {
    // TODO Auto-generated method stub
    Log.v("ChromeClient", "invoked: onConsoleMessage() - " + sourceID + ":"
            + lineNumber + " - " + message);
    super.onConsoleMessage(message, lineNumber, sourceID);
}

@Override
public boolean onConsoleMessage(ConsoleMessage cm) {
    Log.v("ChromeClient", cm.message() + " -- From line "
            + cm.lineNumber() + " of "
            + cm.sourceId() );
    return true;
}

调试问题..