Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: HttpURLConnection executing two times

  1. #1
    Junior Member
    Join Date
    Mar 2022
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default HttpURLConnection executing two times

    I am using HttpURLConnection to make a request to server and save some data in the Database, but when I make the request it executes two times which adds two identical rows in the database.

    Note: I am making the same request to the server from iOS as well and it works perfectly this only happens on Android using Java

    This is the code where I make the request:

    HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();

    httpURLConnection.setRequestMethod("POST");
    httpURLConnection.setDoOutput(true);
    httpURLConnection.setDoInput(true);

    OutputStream outputStream = (OutputStream)httpURLConnection.getOutputStream();
    BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));

    String post_data = URLEncoder.encode("user_name" ,"UTF-8") + "=" + URLEncoder.encode(user,"UTF-8")+"&"
    +URLEncoder.encode("user_id" ,"UTF-8") + "=" + user_id+"&"
    +URLEncoder.encode("manager_id" ,"UTF-8") + "=" + manager+"&"
    +URLEncoder.encode("company_id" ,"UTF-8") + "=" + company+"&"
    +URLEncoder.encode("user_role" ,"UTF-8") + "=" + URLEncoder.encode(user_role ,"UTF-8");

    bufferedWriter.write(post_data);
    bufferedWriter.close();
    outputStream.close();

    InputStream inputStream = httpURLConnection.getInputStream();
    Reader reader = new InputStreamReader(inputStream);

    final char[] buf = new char[256];

    final StringBuffer sb = new StringBuffer();

    while (true) {
    int length = reader.read(buf);
    if (length == -1) break;
    sb.append(buf, 0, length);
    }

    reader.close();
    inputStream.close();

  2. #2
    Junior Member
    Join Date
    Mar 2022
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default HttpURLConnection executing two times

    I am using HttpURLConnection to make a request to server and save some data in the Database, but when I make the request it executes two times which adds two identical rows in the database.

    Note: I am making the same request to the server from iOS as well and it works perfectly this only happens on Android using Java

    This is the code where I make the request:

    HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
     
    httpURLConnection.setRequestMethod("POST");
    httpURLConnection.setDoOutput(true);
    httpURLConnection.setDoInput(true);
     
    OutputStream outputStream = (OutputStream)httpURLConnection.getOutputStream();
    BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
     
    String post_data = URLEncoder.encode("user_name" ,"UTF-8") + "=" + URLEncoder.encode(user,"UTF-8")+"&"
    +URLEncoder.encode("user_id" ,"UTF-8") + "=" + user_id+"&"
    +URLEncoder.encode("manager_id" ,"UTF-8") + "=" + manager+"&"
    +URLEncoder.encode("company_id" ,"UTF-8") + "=" + company+"&"
    +URLEncoder.encode("user_role" ,"UTF-8") + "=" + URLEncoder.encode(user_role ,"UTF-8");
     
    bufferedWriter.write(post_data);
    bufferedWriter.close();
    outputStream.close();
     
    InputStream inputStream = httpURLConnection.getInputStream();
    Reader reader = new InputStreamReader(inputStream);
     
    final char[] buf = new char[256];
     
    final StringBuffer sb = new StringBuffer();
     
    while (true) {
    int length = reader.read(buf);
    if (length == -1) break;
    sb.append(buf, 0, length);
    }
     
    reader.close();
    inputStream.close();

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: HttpURLConnection executing two times

    it executes two times
    Is that because it is called two times? The posted code does not show how it is called.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Mar 2022
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HttpURLConnection executing two times

    Quote Originally Posted by Norm View Post
    Is that because it is called two times? The posted code does not show how it is called.

    This is how I call it, SaveCheckin is AsyncTask class:
    private void saveCheckin(){
            try {
                JSONObject user = new JSONObject(i.getStringExtra("userData"));
     
                String name, id, manager, company, added_by;
                name = user.getString("first_name") +"_"+ user.getString("last_name");
                id = user.getString("ID");
     
                if(is_admin){
                    manager = id;
                    company = id;
                    added_by = "Owner";
     
                }else{
                    manager = user.getString("manager");
                    company = user.getString("company_id");
                    added_by = user.getString("added_by");
                }
     
                SaveCheckin checkin = new SaveCheckin(this, name, buttons, loading, checkinPage, userPhoto);
     
                checkin.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, id, manager, company, added_by, location, userRole);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    And this is the onBackground method of SaveCheckin:
    @Override
        protected String doInBackground(String... strings) {
     
            Date date = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-M-dd HH:mm:ss");
     
            user_id = Integer.parseInt(strings[0]);
            manager = Integer.parseInt(strings[1]);
            company = Integer.parseInt(strings[2]);
     
            String added_by = strings[3];
            String user_role = strings[5];
     
            String url = context.getString(R.string.web_path)+"/mobile/save-data.php";
     
     
            try{
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
     
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
     
                OutputStream outputStream = (OutputStream)httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
     
                String post_data = URLEncoder.encode("user_name" ,"UTF-8") + "=" + URLEncoder.encode(user,"UTF-8")+"&"
                        +URLEncoder.encode("user_id" ,"UTF-8") + "=" + user_id+"&"
                        +URLEncoder.encode("manager_id" ,"UTF-8") + "=" + manager+"&"
                        +URLEncoder.encode("company_id" ,"UTF-8") + "=" + company+"&"
                        +URLEncoder.encode("user_role" ,"UTF-8") + "=" + URLEncoder.encode(user_role ,"UTF-8");
     
                bufferedWriter.write(post_data);
                bufferedWriter.close();
                outputStream.close();
     
                InputStream inputStream = httpURLConnection.getInputStream();
                Reader reader = new InputStreamReader(inputStream);
     
                final char[] buf = new char[256];
     
                final StringBuffer sb = new StringBuffer();
     
                while (true) {
                    int length = reader.read(buf);
                    if (length == -1) break;
                    sb.append(buf, 0, length);
                }
     
                reader.close();
                inputStream.close();
     
                return sb.toString();
            }catch (MalformedURLException e){
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
     
     
            return null;
        }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: HttpURLConnection executing two times

    How do you know that the code in the doInBackground method is being executed two times? Do you have a print statement at the start of the method that prints a message when the method's code is executed?
    If the method is being executed two times, where is it called from each time?
    I do not see any calls to doInBackground in the posted code.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Mar 2022
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HttpURLConnection executing two times

    Quote Originally Posted by Norm View Post
    How do you know that the code in the doInBackground method is being executed two times? Do you have a print statement at the start of the method that prints a message when the method's code is executed?
    If the method is being executed two times, where is it called from each time?
    I do not see any calls to doInBackground in the posted code.
    This line
    checkin.executeOnExecutor(AsyncTask.THREAD_POOL_EX ECUTOR, id, manager, company, added_by, location, userRole);
    calls doInBackground.
    It adds two rows at once in the MySql database.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: HttpURLConnection executing two times

    It adds two rows at once in the MySql database.
    Where does it do that?
    How are you trying to debug the code to see what is added and when it is added to the database?
    Add some print statements that print out the values of variables as they are changed.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Executing cmd from Java, Compiling and Executing IN Runtime
    By Andrew R in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 9th, 2013, 10:00 AM
  2. Java HTTPURLConnection
    By mike2033 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: May 1st, 2013, 04:33 PM
  3. HttpURLConnection doesnt quite cut it
    By chopficaro in forum Java Theory & Questions
    Replies: 2
    Last Post: August 23rd, 2012, 08:48 AM

Tags for this Thread