Facing Errors In Simple HTTP Request

Hello friends

I am new in java, I am creating a extension with extension templete. I want to send HTTP Request but i am facing errors in this

Build Log

Build Log

My Code

@SimpleFunction(description = "method")
public void GenerateStrongPassword() {
URL url = new URL("https://www.passwordrandom.com/query?command=password");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
Value(getInputStream());

}

@SimpleEvent(description = "event")
public void GotPassword(String values){ EventDispatcher.dispatchEvent(this, "Value", values);
}

Hi @Techno_Vedang,
I think your line must be something like that:

Value(con.getInputStream());

instead of:

1 Like

Thanks @MohamedTamer i will check it

1 Like

You're welcome :heart_eyes:

1 Like

EventDispatcher must be called by the same name as the enclosing method. See diff below

@SimpleEvent(description = "event")
public void GotPassword(String values){
-EventDispatcher.dispatchEvent(this, "Value", values);
+EventDispatcher.dispatchEvent(this, "GotPassword", values);
}
4 Likes

Thanks @pavi2410 for your guidance

Thanks @pavi2410 & @MohamedTamer The error is solved but now i want to convert inputstream to string so how can i convert it

1 Like

use

Value(con.getInputStream().toString);
1 Like


@mohamed_tamer

1 Like

Sorry, that was wrong.try this :slightly_smiling_face::

String inputLine;
String output = "";
        while ((inputLine = in.readLine()) != null) {
            output = output + inputLine;
}
Value( output );
1 Like

but where is inputstream

1 Like

i think it should rd.readLine But i don't know much more i am trying this first time

1 Like

The input stream is the

Which is rd with your code.
So You can change it the code to

    String inputLine;
In
    String output = "";
            while ((inputLine = rd.readLine()) != null) {
                output = output + inputLine;
    }
    Value( output );
1 Like

yeah, thanks

1 Like

now i have 5 errors :sob: :sob:
can i pm you my code??

1 Like

Yes you can

1 Like

sent

1 Like

Be aware that on Android 2.3 and higher you will likely get a NetworkOnMainThreadException due to the fact that you're trying to do an HTTP request on the main thread. Typically you need to launch the HTTP request on a separate thread using something like AsynchUtil. See the Web component as an example.

1 Like

@ewpatton thanks sir for your reply. I will try your suggestion

Hi @ewpatton sir I again tried to do as you said but my app is crashing after exporting.

public void Get(final String website) {
    AsynchUtil.runAsynchronously(new Runnable() {
        @Override
        public void run() {
            try {
                URL url = new URL(website);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("GET");
                BufferedReader bufferedReader = null;
                if (httpURLConnection.getResponseCode() == 200) {
                    bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
                } else {
                    bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getErrorStream()));
                }
                StringBuilder stringBuilder = new StringBuilder();
                String str;
                while ((str = bufferedReader.readLine()) != null) stringBuilder.append(str);
                bufferedReader.close();

                final String data = stringBuilder.toString();
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Getdata(data);
                    }
                });
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

}