我想通过本地服务器更新我的应用。我已将我的应用程序放在服务器上,通过此代码我正在完成更新它的任务
public class UpdateClass extends AsyncTask<String, String, String> {
ProgressDialog progressDialog;
int status = 0;
private Context context;
public void onPreExecute() {
progressDialog = new ProgressDialog(Update.this);
progressDialog.setMessage("Please Wait.......");
progressDialog.show();
}
@Override
protected String doInBackground(String... arg0) {
try {
URL url = new URL(arg0[0]);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
// String PATH = Environment.getExternalStorageDirectory() + "/Download/";
String dir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/";
System.out.println("path..........." + Environment.getExternalStorageDirectory());
File file = new File(dir);
file.mkdirs();
File outputFile = new File(file, "location.apk");
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() +
"/Download/" + "location.apk")), "application/vnd.android.package-archive");
System.out.println("path........dsffffffffffffffsdfffff..." + Environment.getExternalStorageDirectory());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} catch (FileNotFoundException fnfe) {
status = 1;
// Toast.makeText(context, "App not Available", Toast.LENGTH_LONG).show();
Log.e("File", "FileNotFoundException! " + fnfe);
} catch (Exception e) {
// Toast.makeText(context, "App update", Toast.LENGTH_LONG).show();
Log.e("UpdateAPP", "Exception " + e);
}
return null;
}
public void onPostExecute(String unused) {
progressDialog.dismiss();
/* if (status == 1)
Toast.makeText(context, "App not Available", Toast.LENGTH_LONG).show();
}*/
}
}
代码可以正常使用“http://localhost/androidservices/location.apk” 但是当我用我的服务器地址https://liveserver/androidservices/location.apk替换这个URL时,我在我的logcat和手机中找到了文件未找到异常,下载了一个大小为0 kb的文件名相同的文件。我可以通过浏览器访问该URL,因为使用浏览器(chrome,mozilla)打开时可以轻松下载该文件。