androidmultithreadingsynchronizedui-threadandroid-runonuithread

Android - Call Thread synchronized on UI thread


I try to create synchronized threads, but I always get the following error: android.os.NetworkOnMainThreadException.

I've read more posts, but they don't work for me.

Below I write the code blocks that do not work for me:

1.

final SyncApp syncJob = new SyncApp();
Thread t = new Thread (new Runnable () {
                         @Override
                         public void run () {
                             synchronized (syncJob) {
                                 String s = syncJob.insert (newJobs, GlobalVariables.URL_LOCALHOST + "jobs");
                                 txtState.setText (s);
                             }}});
                         }
                     });
                     t.Start ();
// t.run ();

2.

myClass.runOnUiThread(new Runnable() {
        public void run() {...}
})

3.

Running code in main thread from another thread

SyncApp:

public class SyncApp {

    synchronized public String insert(List<Jobs> job, String... params) {
        URL url = null;
        HttpURLConnection conn = null;
        try {
            url = new URL(params[0]);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            String str = new Gson().toJson(job);
            byte[] outputInBytes = str.getBytes();
            OutputStream os = conn.getOutputStream();
            os.write( outputInBytes );
            os.flush();

            int responseCode=conn.getResponseCode();
            String response = null;
            if (responseCode == HttpsURLConnection.HTTP_OK) {
                String line;
                BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
                while ((line=br.readLine()) != null) {
                    response+=line;
                }
            }
            else {
                response=conn.getResponseMessage();

            }
            return response;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            conn.disconnect();
        }
        return null;
    }
}

I need to call a thread, wait for the answer and call another thread. Their answers I must use them in the activity


Solution

  • My solution is:

    public class Sync extends AppCompatActivity {
    
    ...
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sync_server);
    
        dao = new DAO(this);
    
        txtState = findViewById(R.id.txt_log);
    
        btnSincro = findViewById(R.id.btn_sincro);
        btnSincro.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                countCall = 0;
                callFlow();
            }
        });
    
        btnHome = findViewById(R.id.btn_home);
        btnHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SyncServerActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }
    
    private void callFlow() {
        switch (countCall) {
    
            case 0:
                templates = toTemplate("url");
                break;
    
            case 1:
                jobs = toJobs("url");
                break;
    
            case 2:
                job = ... //select item
                res = sendJobs(jobs, "url");
                break;
    
            default:
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        btnSincro.setEnabled(true);
                        txtState.append("\n\nEND");
                    }
                });
        }
    }
    
    private void nextStep() {
        setText(txtState, "\nSync \n" + countCall + "/3");
        countCall++;
        callFlow();
    }
    
    private void setText(final TextView text, final String value) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                text.setText(value);
            }
        });
    }
    
    
    public List<Templates> toTemplate(final String... params) {
        final List<Templates> list = new ArrayList<>();
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                URL url = null;
                BufferedReader reader = null;
                HttpURLConnection connection = null;
    
                try {
                    url = new URL(params[0]);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.connect();
    
                    InputStream stream = connection.getInputStream();
    
                    reader = new BufferedReader(new InputStreamReader(stream));
    
                    int responseCode = connection.getResponseCode();
                    String response = null;
                    if (responseCode == HttpsURLConnection.HTTP_OK) {
                        StringBuffer buffer = new StringBuffer();
                        String line = "";
                        while ((line = reader.readLine()) != null) {
                            buffer.append(line);
                        }
                        String finalJson = buffer.toString();
                        JSONObject parentObject = new JSONObject(finalJson);
                        JSONArray parentArray = parentObject.getJSONArray("data");
                        for (int i = 0; i < parentArray.length(); i++) {
                            Templates item = new Gson().fromJson(parentArray.get(i).toString(), Templates.class);
                            list.add(item);
                        }
                    } else {
                        response = connection.getResponseMessage();
                    }
    
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                } finally {
                    if (connection != null)
                        connection.disconnect();
                    try {
                        if (reader != null)
                            reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    nextStep(); //call next Thread
                }
            }
        });
        t.start();
        return list;
    }
    
    public List<Jobs> toJobs(final String... params) {
        final List<Jobs> list = new ArrayList<>();
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                URL url = null;
                BufferedReader reader = null;
                HttpURLConnection connection = null;
                try {
                    url = new URL(params[0]);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.connect();
    
                    InputStream stream = connection.getInputStream();
    
                    reader = new BufferedReader(new InputStreamReader(stream));
    
                    int responseCode = connection.getResponseCode();
                    String response = null;
                    if (responseCode == HttpsURLConnection.HTTP_OK) {
                        StringBuffer buffer = new StringBuffer();
                        String line = "";
                        while ((line = reader.readLine()) != null) {
                            buffer.append(line);
                        }
                        String finalJson = buffer.toString();
                        JSONObject parentObject = new JSONObject(finalJson);
                        JSONArray parentArray = parentObject.getJSONArray("data");
                        for (int i = 0; i < parentArray.length(); i++) {
                            Jobs item = new Gson().fromJson(parentArray.get(i).toString(), Jobs.class);
                            list.add(item);
                        }
                    } else {
                        response = connection.getResponseMessage();
                    }
    
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                } finally {
                    if (connection != null)
                        connection.disconnect();
                    try {
                        if (reader != null)
                            reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    nextStep();
                }
            }
        });
        t.start();
        return list;
    }
    
    public Boolean sendJobs(final List<Jobs> job, final String... params) {
        final Boolean[] result = {false};
        Thread t = new Thread(new Runnable() {
    
            @Override
            public void run() {
                URL url = null;
                HttpURLConnection conn = null;
                try {
                    url = new URL(params[0]);
                    conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("POST");
                    conn.setRequestProperty("Content-Type", "application/json");
                    conn.setDoInput(true);
                    conn.setDoOutput(true);
                    String str = new Gson().toJson(job);
                    Log.d(TAG, str);
                    byte[] outputInBytes = str.getBytes();
                    OutputStream os = conn.getOutputStream();
                    os.write(outputInBytes);
                    os.flush();
    
                    int responseCode = conn.getResponseCode();
                    String response = null;
                    if (responseCode == HttpsURLConnection.HTTP_OK) {
                        String line;
                        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                        while ((line = br.readLine()) != null) {
                            response += line;
                        }
                        result[0] = true;
                    } else {
                        response = conn.getResponseMessage();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    conn.disconnect();
                    nextStep();
                }
            }
        });
        t.start();
        return result[0];
    }
    }
    

    Whenever a thread ends, it calls the nextStep() method, which starts the next trhead.