0

I'm using code from Android: install .apk programmatically .

Im trying to receive data from server, to updatate my app.

After I click Button app crashes.

If you know hot to fix these errors tell me.

I get this errors:

android.os.NetworkOnMainThreadException

at com.example.appupdate.SelfInstall01Activity.GetVersionFromServer(SelfInstall01Activity.java:285)

at com.example.appupdate.SelfInstall01Activity$1.onClick(SelfInstall01Activity.java:89)

Ive got errors there:

btnCheckUpdates.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
            GetVersionFromServer(BuildVersionPath);
           //here

            if(checkInstalledApp(AppName.toString()) == true)
            {
                Toast.makeText(getApplicationContext(), "Application Found " + AppName.toString(), Toast.LENGTH_SHORT).show();


            }else{
                Toast.makeText(getApplicationContext(), "Application Not Found. "+ AppName.toString(), Toast.LENGTH_SHORT).show();
            }
        }
    });

And:

HttpURLConnection c = (HttpURLConnection) u.openConnection();
    c.setRequestMethod("GET");
    c.setDoOutput(true);
    c.connect();
    //here

Here is rest of code:

public void GetVersionFromServer(String BuildVersionPath)
{
URL u;
    try {
        u = new URL(BuildVersionPath.toString());

        HttpURLConnection c = (HttpURLConnection) u.openConnection();
        c.setRequestMethod("GET");
        c.setDoOutput(true);
        c.connect();

        InputStream in = c.getInputStream();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024]; 


        int len1 = 0;
        while ( (len1 = in.read(buffer)) != -1 )
        {
            baos.write(buffer,0, len1); 
        }

        String temp = "";
        String s = baos.toString();

        for (int i = 0; i < s.length(); i++)
        {
            i = s.indexOf("=") + 1;
            while (s.charAt(i) == ' ')
            {
                i++; 
            }
            while (s.charAt(i) != ';'&& (s.charAt(i) >= '0' && s.charAt(i) <= '9' || s.charAt(i) == '.'))
            {
                temp = temp.toString().concat(Character.toString(s.charAt(i))) ;
                i++;
            }
            //
            s = s.substring(i); 
            temp = temp + " "; 
        }
        String[] fields = temp.split(" ");

        VersionCode = Integer.parseInt(fields[0].toString());
        VersionName = fields[1].toString();

        baos.close();
    }
Peter M.
  • 23
  • 4

2 Answers2

0

NetworkOnMainThreadException occurs when you perform network operation in main thread.. you need to perform your network operation in doInBackground method

here your button click function should be like this.

btnCheckUpdates.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        { 
           new YourTask().execute();
        }
    });

And here is your Your network operation.

private class YourTask extends AsyncTask<String, String,String> {
     protected String doInBackground(String... urls) {

         // put here your network operation method
           GetVersionFromServer(BuildVersionPath)

        return null;
     }

 }
Vikas singh
  • 3,461
  • 3
  • 14
  • 28
  • I did it but i have errors in: // put here your network operation method GetVersionFromServer(String BuildVersionPath) and in button: new GetVersionFromServer(BuildVersionPath).execute(); – Peter M. Jun 23 '18 at 12:57
  • you need to put `new YourTask().execute();` in your button click..check my updated code.. – Vikas singh Jun 25 '18 at 07:10
0

NetworkOnMainThreadException will be thrown when you make network request in UI/Main thread. So you need to call GetVersionFromServer() off the main thread. Use Threads or AsyncTask.

pop
  • 559
  • 3
  • 13