0

我的 HttpUrlConnection 正在发出 2 个请求,因为我只是在我的代码中发送一次。

下面是我的代码:

HttpURLConnection urlConnection = null;
                    try
                    {


                        URL myUrl = new URL("http://" + url);
                        urlConnection = (HttpURLConnection) myUrl.openConnection();
                        urlConnection.setRequestMethod("GET");
                        urlConnection.setChunkedStreamingMode(0);
                        urlConnection.setRequestProperty("Accept-Encoding", "");
                        urlConnection.setRequestProperty("my-header", header);
                        int code = urlConnection.getResponseCode();

                        if(code != -1)
                        {
                            wv.loadUrl(myUrl.toString());
                        }
                        else
                        {
                            wv.loadUrl("http://www.google.com/search?q=" + url);
                            et_URL.setText(wv.getUrl());

                }  

当我看到来自服务器的日志时,它会显示 2 个 GET 请求。
任何身体任何想法?

4

3 回答 3

0

第一个 GET 请求由这一行发起:

int code = urlConnection.getResponseCode();

第二个 GET 请求发生在 WebView 加载 URL 时:

wv.loadUrl(myUrl.toString());
于 2015-04-09T07:52:42.370 回答
0

http://www.javacodegeeks.com/2013/06/android-http-client-get-post-download-upload-multipart-request.html

嗨,它是如何在 Android 中使用 HTTP 请求的一个很好的例子,我更喜欢使用 Volley 库,它很容易实现:)

    private String RegisterUser(String urlString) {

        ok = true;
        String result = null;

        try {

            Log.d(TAG, "UrlString:" + urlString);

            CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));

            HttpURLConnection con = null;
            URL url = new URL(urlString);
            con = (HttpURLConnection) url.openConnection();
            Log.d(TAG, "open connection");

            con.setReadTimeout(1000);
            con.setConnectTimeout(15000);
            con.setRequestMethod("GET");
            con.setDoInput(true);

            // start the query
            con.connect();
            Log.d(TAG, "Connected");

            if (Thread.interrupted())
                throw new InterruptedException();

            InputStream response = con.getInputStream();

            Log.d(TAG, "Response: " + response.toString());

            // Scanner sc = new Scanner(con.getInputStream());

            result = "OK";

            Log.d(TAG, "Result:" + result);

            // sc.close();
            con.disconnect();

            return result;

        } catch (Exception e) {
            // showInfoDialog("Error while accesing the web service");
            Log.d(TAG, e + "");
            return "Error";
        }

    }
于 2013-08-22T09:24:31.320 回答
0

这是我如何在 webview 中加载 url 的简单版本,但如果您对该版本感兴趣,您甚至可以使用资产谷歌搜索 css:

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {

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

        WebView mainWebView = (WebView) findViewById(R.id.mainWebView);

        WebSettings webSettings = mainWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        mainWebView.setWebViewClient(new MyCustomWebViewClient());
        mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

        mainWebView.loadUrl("http://www.google.com");
    }

    private class MyCustomWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}

和xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <WebView 
         android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/mainWebView">
    </WebView>

</LinearLayout>
于 2013-08-22T14:48:30.450 回答