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