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