0

I am using Android Studio 2.2 and I'm trying to see if a web service is up when pressing a button, but I receive the following error. I'm a beginner so any advice is helpful. Thank you very much.

This is the code in android studio. I found something on the internet but I'm not sure what every line is doing.

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);
    }    
}

This is a part of the error I receive when i am pressing the button in app :

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) 

Later Edit: This is my xml file:

<?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>
alexrnov
  • 1,384
  • 2
  • 7
  • 22

3 Answers3

0

Initialise button in 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()));
        }

}

Either use android:onClick="buttonClicked" in you xml for button

or set OnClicklistener on you button

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

public void buttonClicked() {

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

Make network call on background thread You can use AsynTask check this link for network guide

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

Ajesh
  • 128
  • 8
0

Here is a sample how to call via AsyncTask. This code contains also code to pass data to server in JSON (if needed). Also take care about a possible redirection to another URL, as described in my code

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

DownloadAsyncTask declaration:

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() {}

    }
0

When doing a Network Call in android first check in your application Manifest if Internet Permission is given to your application.
If not first provide permission in your application Manifest file like this:

<?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>

Then to do a handshaking request with a server use an AsyncTask. Never do Network Calls on main thread because it will block your main thread and application will freeze until your application receives a response from the server. Use a simple AsyncTask like this:

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;
}
}

Then call it in your button click method.

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

Then it will be fine

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

Nilupul Sandeepa
  • 484
  • 3
  • 14
  • After I press the button I receive no output and after a while the follwing error appears: `11:00:09.936 30749-31073/com.example.roxanapena.myapp6 W/System.err: java.net.ConnectException: Connection refused` – Pena Roxana Mar 04 '19 at 09:03
  • Make sure your url is correct. That might be the problem. And check https://stackoverflow.com/questions/6876266/java-net-connectexception-connection-refused this answer for possible errors – Nilupul Sandeepa Mar 04 '19 at 09:40
  • I tried the URL `https://www.google.com` and still nothing. It doesn't output anything, just this: `D/NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true`. I looked out and found that I have to create a xml file network_security_config and then add `android:networkSecurityConfig="@xml/network_security_config"` in the Manifest. I did this, but I don t know why there is no output. – Pena Roxana Mar 04 '19 at 09:53