I'm trying to post a an HTTP URL with parameters. I've appended the parameters using appendQueryPrameters But statements after build() are skipped and the control comes out of the AsyncTask.Below is th snippet of the AsyncTask
private class MyAsyncTask extends AsyncTask<String, Integer, String> { @Override protected String doInBackground(String... params) { // TODO Auto-generated method stub String givenDob = params[0]; String givensurname = params[1]; String givenCaptcha = params[2]; String response = ""; try { Uri.Builder builder = new Uri.Builder() .appendQueryParameter("dateOfBirth", givenDob) .appendQueryParameter("userNameDetails.surName", givensurname) .appendQueryParameter("captchaCode", givenCaptcha); String query = builder.build().toString(); PrintWriter out = new PrintWriter(connection.getOutputStream()); out.print(query); out.close(); int responseCode = connection.getResponseCode(); Log.d("responseCode", String.valueOf(responseCode)); /* BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(connection.getOutputStream(), "ISO-8859-1")); writer.write(query); writer.flush(); writer.close(); */ connection.getOutputStream().close(); if (responseCode == HttpsURLConnection.HTTP_OK) { String line; BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); while ((line = br.readLine()) != null) { response += line; Log.d("response", response); } } else { response = ""; } } catch (IOException e) { e.printStackTrace(); } return response; } @Override protected void onPostExecute(String s) { Log.d("res", s); } } I tried with PrintWriter also.Still it skips the execution of the statements after the line String query = builder.build().toString();
PS: I've opened the HttpURLconnection in another AsyncTask and calling that on onCreate() Below is the code.
URL url = new URL("https://myurl.com/path1/path2/path3.html"); connection = (HttpsURLConnection) url.openConnection(); connection.setReadTimeout(10000); connection.setConnectTimeout(15000); connection.setRequestMethod("POST"); connection.setDoInput(true); connection.setDoOutput(true); Used this for reference