a_l_e_x
a_l_e_x

Reputation: 400

How to solve Bad URL error in Android studio

I am creating a simple application in android studio for managing a device connected via wifi using the Volley library. When I run the application, I get the following error.

java.lang.RuntimeException: Bad URL 192.168.137.139:8081/zeroconf/switch at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:171) at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:120) at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:87) Caused by: java.net.MalformedURLException: no protocol: 192.168.137.139:8081/zeroconf/switch at java.net.URL.(URL.java:601) at java.net.URL.(URL.java:498) at java.net.URL.(URL.java:447) at com.android.volley.toolbox.HurlStack.executeRequest(HurlStack.java:92) at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:131) at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:120)  at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:87) 

How can I solve?

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.text.BreakIterator;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getName();
    SwitchCompat switchButton;
    LinearLayout imageViewLight;
    private RequestQueue mRequestQueue;
    TextView buttonState;
    private StringRequest mStringRequest;
    private String url = "192.168.137.139:8081/zeroconf/switch";

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

        switchButton=findViewById(R.id.switchButton);
        imageViewLight=findViewById(R.id.linearL);
        TextView buttonState;
        buttonState = findViewById(R.id.buttonState);

        switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                sendAndRequestResponse();                
            }
        });
    }

    private void sendAndRequestResponse() {
        mRequestQueue = Volley.newRequestQueue(this);
        mStringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                TextView myTextView;
                if (switchButton.isChecked()) {
                    imageViewLight.setBackgroundResource(R.drawable.light__02);
                    buttonState.setText("State : ON");
                } else {
                    imageViewLight.setBackgroundResource(R.drawable.light__01);
                    buttonState.setText("State : OFF");
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Log.i(TAG,"Error :" + error.toString());
            }
        });

        mRequestQueue.add(mStringRequest);  

    }
}

Upvotes: 0

Views: 1063

Answers (2)

Nebula Shade
Nebula Shade

Reputation: 77

Try to declare your URL like this:

private String URL = "http://192.168.137.139:8081/zeroconf/switch";

There are some additional setups if you want to make an HTTP request by Volley while using HTTP instead of HTTPS. If you are working with HTTP add this to your Manifest:

        <uses-library
        android:name="org.apache.http.legacy"
        android:required="false" />

And create this XML file:

<?xml version="1.0" encoding="utf-8"?>
  <network-security-config xmlns:android="http://schemas.android.com/apk/res                 /android">
<base-config cleartextTrafficPermitted="true" />
</network-security-config>

Don't forget to test your URL using Postman to make sure your URL is working or not.

Upvotes: 1

Yash Shah
Yash Shah

Reputation: 60

I had a similar issue and it got resolved by calling MoPub.initializeSdk() on a java side.

Upvotes: 0

Related Questions