Java 两个异步任务在android studio中不能同时工作

Java 两个异步任务在android studio中不能同时工作,java,android,multithreading,android-asynctask,Java,Android,Multithreading,Android Asynctask,第一个异步任务-从服务器获取映像数据 并将其显示到图像查看器中。(查看代码)第二 异步-始终运行。它将检查来自的更新 如果更新的密钥为true,则为服务器(例如:图片是否更新) (意味着更新),然后它将再次获取数据,否则它将不这样做 任何东西但在后台,第二个异步任务将持续运行。 我使用这个代码,但它不工作。帮帮我 公共类ImageActivity扩展了AppCompatActivity{ 私有图像视图img; 位图解码; 私有字符串pureFile; 私有字符串数据; 私人JSONObject

第一个异步任务-从服务器获取映像数据 并将其显示到图像查看器中。(查看代码)第二 异步-始终运行。它将检查来自的更新 如果更新的密钥为true,则为服务器(例如:图片是否更新) (意味着更新),然后它将再次获取数据,否则它将不这样做 任何东西但在后台,第二个异步任务将持续运行。 我使用这个代码,但它不工作。帮帮我


公共类ImageActivity扩展了AppCompatActivity{
私有图像视图img;
位图解码;
私有字符串pureFile;
私有字符串数据;
私人JSONObject post_dict2;
私有布尔集;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,
WindowManager.LayoutParams.FLAG(全屏);
setContentView(R.layout.activity\u图像);
img=(ImageView)findViewById(R.id.img);
post_dict2=新的JSONObject();
试一试{
post_dict2.put(“设备”,“ABCDEFGHIJ”);
}捕获(JSONException e){
e、 printStackTrace();
}
if(post_dict2.length()>0){
新建jsonTask().execute(String.valueOf(post_dict2));
}
}
公共类jsonTask扩展了异步任务{
字符串扩展;
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串JsonResponse=null;
字符串JsonDATA=params[0];
HttpURLConnection HttpURLConnection=null;
BufferedReader BufferedReader=null;
试一试{
URL=新URL(“”);
httpURLConnection=(httpURLConnection)url.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod(“POST”);
setRequestProperty(“内容类型”、“应用程序/json”);
setRequestProperty(“接受”、“应用程序/json”);
setRequestProperty(“X-request-With”,“XMLHttpRequest”);
Writer Writer=new BufferedWriter(new OutputStreamWriter(httpURLConnection.getOutputStream(),“UTF-8”);
writer.write(JsonDATA);
writer.close();
InputStream InputStream=httpURLConnection.getInputStream();
bufferedReader=新的bufferedReader(新的InputStreamReader(inputStream));
StringBuffer StringBuffer=新的StringBuffer();
字符串行=”;
而((line=bufferedReader.readLine())!=null){
stringBuffer.append(行);
}
String file=stringBuffer.toString();
JSONObject filejsonObject=新的JSONObject(文件);
JSONObject metaObject=filejsonObject.getJSONObject(“meta”);//JSON文件中的元数据
data=filejsonObject.getString(“数据”);//这是对象的主数据键
ext=metaObject.getString(“ext”);
返回数据;
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串s){
字符串run=“run”;
//s是图像源数据
如果(s!=null){
字节[]decodeString=Base64.decode(s,Base64.DEFAULT);
试一试{
//在设备文件存储(即媒体存储)中创建新文件夹
File folder=新文件(Environment.getExternalStorageDirectory()+“/ImgDownload”);
布尔成功=真;
如果(!folder.exists()){
success=folder.mkdir();
}
如果(成功){
设置=真;
FileOutputStream out=新的FileOutputStream(文件夹+“/”+“.png”);
out.write(解码字符串);
out.flush();
out.close();
Uri=Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+”/ImgDownload/“+”.png”);
setImageURI(uri);
睡眠(10000);
while(run.equals(“run”)){
new updateFile().executeOnExecutor(AsyncTask.THREAD\u POOL\u EXECUTOR,String.valueOf(post\u dict2));
//
}
}否则{
Toast.makeText(ImageActivity.this,“文件保存失败”,Toast.LENGTH_LONG.show();
}
}捕获(例外e){
Log.e(“Error”,e.toString());
}
}否则{
Toast.makeText(ImageActivity.this,“下载失败”,Toast.LENGTH_LONG.show();
}
}
}
私有类updateFile扩展了异步任务{
字符串扩展;
字符串媒体;
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串JsonResponse=null;
字符串JsonDATA=params[0];
//字符串媒体=参数[1];
HttpURLConnection HttpURLConnection=null;
BufferedReader BufferedReader=null;
试一试{
睡眠(10000);
URL=新URL(“myurl”);
httpURLConnection=(httpURLConnection)url.openConnection();
httpURLConnection.setDoOutput
public class ImageActivity extends AppCompatActivity {
    private ImageView img;
    Bitmap decoded;
    private String pureFile;
    private String data;
    private JSONObject post_dict2;
    private boolean set;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_image);

        img = (ImageView) findViewById(R.id.img);


        post_dict2 = new JSONObject();

        try {
            post_dict2.put("device", "ABCDEFGHIJ");
        } catch (JSONException e) {
            e.printStackTrace();
        }
         if (post_dict2.length() > 0) {
            new jsonTask().execute(String.valueOf(post_dict2));

        }


    }


    public class jsonTask extends AsyncTask<String, String, String> {

        String ext;


        @Override
        protected String doInBackground(String... params) {
            String JsonResponse = null;
            String JsonDATA = params[0];
            HttpURLConnection httpURLConnection = null;
            BufferedReader bufferedReader = null;

            try {
                URL url = new URL("");
                httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setRequestProperty("Content-Type", "application/json");
                httpURLConnection.setRequestProperty("Accept", "application/json");
                httpURLConnection.setRequestProperty("X-Requested-With", "XMLHttpRequest");

                Writer writer = new BufferedWriter(new OutputStreamWriter(httpURLConnection.getOutputStream(), "UTF-8"));
                writer.write(JsonDATA);

                writer.close();


                InputStream inputStream = httpURLConnection.getInputStream();
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuffer stringBuffer = new StringBuffer();
                String line = " ";
                while ((line = bufferedReader.readLine()) != null) {


                    stringBuffer.append(line);

                }


                String file = stringBuffer.toString();

                JSONObject filejsonObject = new JSONObject(file);
                JSONObject metaObject = filejsonObject.getJSONObject("meta"); //meta data from JSON file
                data = filejsonObject.getString("data"); //this is the main data key from object
                ext = metaObject.getString("ext");


                return data;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }


            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            String run = "run";

            //s is the image source data
            if (s != null) {
                byte[] decodeString = Base64.decode(s, Base64.DEFAULT);
                try {
                    // Creating a new folder in to the device files storage i.e. media storage
                    File folder = new File(Environment.getExternalStorageDirectory() + "/ImgDownload");
                    boolean success = true;
                    if (!folder.exists()) {
                        success = folder.mkdir();
                    }
                    if (success) {
                        set = true;
                        FileOutputStream out = new FileOutputStream(folder + "/" + ".png");
                        out.write(decodeString);
                        out.flush();
                        out.close();
                        Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ImgDownload/" + ".png");
                        img.setImageURI(uri);

                        Thread.sleep(10000);
                        while(run.equals("run")){

                                    new updateFile().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, String.valueOf(post_dict2));

//
                        }



                    } else {

                        Toast.makeText(ImageActivity.this, "File Saving Failed", Toast.LENGTH_LONG).show();

                    }
                } catch (Exception e) {
                    Log.e("Error", e.toString());
                }
            } else {


                Toast.makeText(ImageActivity.this, "Failed To Download", Toast.LENGTH_LONG).show();
            }


        }


    }


        private class updateFile extends AsyncTask<String, String, String> {


            String ext;
            String media;

            @Override
            protected String doInBackground(String... params) {

                String JsonResponse = null;
                String JsonDATA = params[0];
               // String media = params[1];
                HttpURLConnection httpURLConnection = null;
                BufferedReader bufferedReader = null;

                try {
                    Thread.sleep(10000);
                    URL url = new URL("myurl");
                    httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setDoOutput(true);
                    httpURLConnection.setRequestMethod("POST");
                    httpURLConnection.setRequestProperty("Content-Type", "application/json");
                    httpURLConnection.setRequestProperty("Accept", "application/json");
                    httpURLConnection.setRequestProperty("X-Requested-With", "XMLHttpRequest");

                    Writer writer = new BufferedWriter(new OutputStreamWriter(httpURLConnection.getOutputStream(), "UTF-8"));
                    writer.write(JsonDATA);

                    writer.close();


                    InputStream inputStream = httpURLConnection.getInputStream();
                    bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                    StringBuffer stringBuffer = new StringBuffer();
                    String line = " ";
                    while ((line = bufferedReader.readLine()) != null) {


                        stringBuffer.append(line);

                    }


                    String file = stringBuffer.toString();

                    JSONObject filejsonObject = new JSONObject(file);
                    JSONObject metaObject = filejsonObject.getJSONObject("data"); //meta data from JSON file
                    //   data = filejsonObject.getString("data"); //this is the main data key from object
                    ext = metaObject.getString("updated");
                    if (ext.equals("true")) {
                       Intent in = new Intent(ImageActivity.this,MainActivity.class);
                        startActivity(in);

                    }

                    else {

                        Thread.sleep( 1 ); //as the pic is not update and stop this async task 
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }


                return null;
            }


        }

            }