0

I have this exception and I was reading a thread on this, and it seemed confusing:

I already added this line to my manifest:

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

I have a function which is getting a url a returning a string but it gives an error of network thread exception. I searched about it ad i got the solution that i should use async task but problem is i cannot figure out how to use the function using async task and return a string This is my function

private String createEnterprise() throws IOException {
    // Initiate signup process.
    System.out.println("Creating signup URL...");
    final SignupUrl[] signupUrl = androidManagementClient
                                .signupUrls()
                                .create()
                                .setProjectId(PROJECT_ID)
                                .setCallbackUrl("https://localhost:80")
                                .execute();

    System.out.print(
            "To sign up for a new enterprise, open this URL in your browser: ");
    System.out.println(signupUrl[0].getUrl());
    System.out.println(
            "After signup, you will see an error page in the browser.");
    System.out.print(
            "Paste the enterpriseToken value from the error page URL here: ");
    String enterpriseToken =
            new BufferedReader(new InputStreamReader(System.in)).readLine();

    // Create the enterprise.
    System.out.println("Creating enterprise...");
    return androidManagementClient
            .enterprises()
            .create(new Enterprise())
            .setProjectId(PROJECT_ID)
            .setSignupUrlName(signupUrl[0].getName())
            .setEnterpriseToken(enterpriseToken)
            .execute()
            .getName();
}

and here i am using this function in a string

String enterpriseName = createEnterprise();

Please help to figure out how to use with a async task or some other method

twana eng
  • 31
  • 10
  • Your code is not written for Android. It is attempting to get user input via text typed at a terminal window (`System.in`). Android does not have a terminal window. Beyond that, there has been a *lot* written about `NetworkOnMainThreadException`, including the 58 answers on the duplicate question. For example, the documentation has sections like [this one](https://developer.android.com/training/multiple-threads). If you have problems with some specific technique, ask a question with a [mcve] showing what you tried and what specific problems you encountered. – CommonsWare Mar 26 '20 at 11:30
  • well i have resolved this nework exception and u r right this code is not written for android..what do u suggest if i want to write this code for android – twana eng Mar 26 '20 at 12:14

0 Answers0