繁体   English   中英

如何检查Web服务器是否已启动?

[英]How to check web server is up?

我正在使用 Android Studio 2.2 并且我试图在按下按钮时查看网络服务是否已启动,但我收到以下错误。 我是初学者,所以任何建议都是有帮助的。 非常感谢。

这是android studio中的代码。 我在互联网上找到了一些东西,但我不确定每一行都在做什么。

package com.example.roxanapena.myapp6;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;    
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.HttpCookie;
import static java.net.Proxy.Type.HTTP;


public class MainActivity extends AppCompatActivity {

    public boolean available() throws IOException {

        URL url = new URL("https://www.Google.com");

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setDoOutput(true);
        connection.setRequestMethod("POST");

        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            System.out.println("OK");
            return true;
            // otherwise, if any other status code is returned, or no status
            // code is returned, do stuff in the else block
        } else {
            System.out.println("WRONG");
            return false;
            // Server returned HTTP error code.
        }    
    }
    Button button3;
    public void buttonClicked(View view) throws IOException {

        button3 = (Button) findViewById(R.id.button3);
        EditText mytextField = (EditText) findViewById(R.id.mytextField);

        Log.i("Ok", String.valueOf(available()));    
    }
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }    
}

这是我在应用程序中按下按钮时收到的错误的一部分:

03-01 12:13:08.977 28514-28514/com.example.roxanapena.myapp6 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: com.example.roxanapena.myapp6, PID: 28514
                                                                               java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                                   at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                                   at android.view.View.performClick(View.java:5637)
                                                                                   at android.view.View$PerformClick.run(View.java:22429)
                                                                                   at android.os.Handler.handleCallback(Handler.java:751)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                   at android.os.Looper.loop(Looper.java:154)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                                Caused by: java.lang.reflect.InvocationTargetException
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                                   at android.view.View.performClick(View.java:5637) 
                                                                                   at android.view.View$PerformClick.run(View.java:22429) 
                                                                                   at android.os.Handler.handleCallback(Handler.java:751) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                   at android.os.Looper.loop(Looper.java:154) 

稍后编辑:这是我的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/GreenRed"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.roxanapena.myapp6.MainActivity">

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="buttonClicked"
        android:text="Button"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="135dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintLeft_creator="1" />

    <EditText
        android:id="@+id/mytextField"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="142dp"
        android:layout_marginTop="143dp"
        android:ems="10"
        android:hint="Name"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button3"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintBottom_creator="1"
        tools:layout_constraintLeft_creator="1" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:layout_marginBottom="43dp"
        app:layout_constraintBottom_toTopOf="@+id/button3"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="44dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintBottom_creator="1"
        tools:layout_constraintLeft_creator="1" />

</android.support.constraint.ConstraintLayout>

onCreate 中的初始化按钮

public class MainActivity extends AppCompatActivity {

    public boolean available() throws IOException {

            URL url = new URL("http://www.Google.com");

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setDoOutput(true);
            connection.setRequestMethod("POST");

            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                System.out.println("OK");
                 return true;
                // otherwise, if any other status code is returned, or no status
                // code is returned, do stuff in the else block
            } else {
                System.out.println("WRONG");
                 return false;
                // Server returned HTTP error code.
            }

    }
    Button button3;


    @Override
    protected void onCreate (Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button3 = (Button) findViewById(R.id.button3);
        EditText mytextField = (EditText) findViewById(R.id.mytextField);

    }

        public void buttonClicked(View view) throws IOException{

                Log.i("Ok", String.valueOf(available()));
        }

}

要么在你的 xml 中使用android:onClick="buttonClicked"按钮

或在您的按钮上设置OnClicklistener

    button3.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    buttonClicked()
                }
            });

public void buttonClicked() {

                Log.i("Ok", String.valueOf(available()));
        }

在后台线程上进行网络调用您可以使用 AsynTask 检查此链接以获取网络指南

https://developer.android.com/training/basics/network-ops/connecting

这是一个如何通过 AsyncTask 调用的示例。 此代码还包含以 JSON 格式将数据传递到服务器的代码(如果需要)。 还要注意可能重定向到另一个 URL,如我的代码中所述

handShakeResponse = new DownloadAsyncTask().execute(request).get();

DownloadAsyncTask 声明:

private class DownloadAsyncTask extends AsyncTask<JSONObject, Void, String> {
        @Override
        protected String doInBackground(JSONObject... params) {
            JSONObject response = null;
            JSONObject request = params[0];
            if (request != null) {

                URL new_url;
                String server_response = null;

                HttpURLConnection urlConnection = null;
                try {
                    new_url = new URL(HANDSHAKE_SERVER);
                    urlConnection = (HttpURLConnection) new_url.openConnection();

                    urlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
                    urlConnection.setRequestMethod("POST");
                    urlConnection.setInstanceFollowRedirects(true);
                    urlConnection.setDoOutput(true);

                    OutputStream os = urlConnection.getOutputStream();
                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "utf-8"));
                    writer.write(request.toString());
                    writer.flush();
                    writer.close();
                    os.close();

                    int response_code = urlConnection.getResponseCode();

                    if(response_code == HttpURLConnection.HTTP_OK) {
                        server_response = readStream(new BufferedInputStream(urlConnection.getInputStream()));

                    } else {
                        Log.e(getClass().getSimpleName(), "response code:" + String.valueOf(response_code));
                        if (response_code == HttpURLConnection.HTTP_MOVED_PERM || response_code == HttpURLConnection.HTTP_MOVED_TEMP) {

                            String server = urlConnection.getHeaderField("Location");

                            if (server == null || server.isEmpty()) {
                                Log.e(TAG, "Redirect not found!");
                            } else {
                                Log.e(TAG, "Redirecting to: " + server);
                                //You must handle here the redirection 
                                return "redirect"; //this is just a flag for me, do your own handling
                            }                               
                        }
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                    return null;
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (urlConnection != null) {
                        urlConnection.disconnect();
                    }
                }

                return server_response;
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {

        }

        @Override
        protected void onCancelled() {
            // Cancel task
        }

        @Override
        protected void onPreExecute() {}

    }

在 android 中进行网络调用时,首先检查您的应用程序清单是否已向您的应用程序授予 Internet 权限。
如果不是首先在您的应用程序清单文件中提供权限,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.truckit">

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".LoginActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainActivity" />
</application>

然后使用 AsyncTask 与服务器进行握手请求。 切勿在主线程上进行网络调用,因为它会阻塞您的主线程并且应用程序将冻结,直到您的应用程序收到来自服务器的响应。 像这样使用一个简单的 AsyncTask:

public class HandShakingTask extends AsyncTask<String, Void, Void> {

@Override
protected Void doInBackground(String... urls) {
    try {
        URL url = new URL(urls[0]);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setRequestMethod("GET");

        System.out.println(connection.getResponseMessage());

        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            System.out.println("OK");
            // otherwise, if any other status code is returned, or no status
            // code is returned, do stuff in the else block
        } else {
            System.out.println("WRONG");
            // Server returned HTTP error code.
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
}

然后在您的按钮单击方法中调用它。

new HandShakingTask().execute("https://www.google.com");

然后就好了

结果:2019-03-02 00:52:55.618 5575-5597/com.truckit I/System.out:OK

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM