UrlConnection发出2个GET请求

时间:2013-08-22 09:16:17

标签: android httpurlconnection

我的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请求 任何身体任何想法??

3 个答案:

答案 0 :(得分: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";
        }

    }

答案 1 :(得分:0)

这是我在网页浏览中加载网址的简单版本,但如果您对该版本感兴趣,可以使用资源进行css google it:

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>

答案 2 :(得分:0)

第一个GET请求由此行启动:

int code = urlConnection.getResponseCode();

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

wv.loadUrl(myUrl.toString());