0

I am trying to send a POST request from an android client to a server. I am able to send a GET request and receive data but so far have failed to send a POST request.
I am getting an exception from the part where I try to form the connection. Can any one point out what am I missing here?
Note: This is the second time I am sending a request, as I mentioned above. So I closed the first connection using connection.disconnect();

public class JSONTask extends AsyncTask<String,String,String> {
        String error = "";
        @Override
        protected String doInBackground(String... params){
            BufferedReader reader = null;
            HttpURLConnection connection = null;

            try {
                URL url = new URL(params[0]);
                connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("POST");
                connection.setDoOutput(true);
                connection.connect();
                OutputStream oStream = connection.getOutputStream();
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(oStream, "UTF-8"));
                writer.write(params[1].toString());
                writer.write(params[2].toString());
                writer.close();

                InputStream stream = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(stream));
                StringBuffer buffer = new StringBuffer();
                String line = "";
                while ((line = reader.readLine()) != null) {
                    buffer.append(line);
                }
                String response = buffer.toString();
                return response;
            }
            catch(Exception e){
                e.printStackTrace();
            }
            finally
            {
                try
                {
                    reader.close();
                    connection.disconnect();
                }
                catch (Exception e){
                    e.printStackTrace();
                    error = e.getMessage();
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result){
            super.onPostExecute(result);
            recvTV.setText(result);
            recvTV.setText(error);
        }
    }


EDIT:This function calls the asyncTask.

public void submitQuestion(View view)
    {
        recvTV = (TextView) findViewById(R.id.recvTV2);
        EditText text = (EditText) findViewById(R.id.qTextEditText);
        EditText sId = (EditText) findViewById(R.id.surveyIdET);
        new JSONTask().execute("http://192.168.168.168:3000/users/submitQuestion",text.getText().toString(), sId.getText().toString() );
    }
ashwin mahajan
  • 1,654
  • 5
  • 27
  • 50
  • why are you setting recvTV.setText twice. and is this AsyncTask in the same Activity Class otherwise recvTV is not accesable – Zar E Ahmer Oct 27 '16 at 06:56
  • also add code for calling this JSONTask – Zar E Ahmer Oct 27 '16 at 06:58
  • Yes, it is an inner class and I tried to display error so I added the second statement. – ashwin mahajan Oct 27 '16 at 06:58
  • do something like this... ` connection.setDoOutput(true); connection.setChunkedStreamingMode(0); OutputStream outputStream = connection.getOutputStream(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); writer.write(uri.getEncodedQuery()); writer.flush(); writer.close(); outputStream.close(); } connection.connect();` – Ahad Oct 27 '16 at 06:59
  • @AbdulAhad It skips the code after `connection.setDoOutput();`. – ashwin mahajan Oct 27 '16 at 07:08
  • Use http://stackoverflow.com/a/27246948/3496570 or http://stackoverflow.com/a/4206094/3496570 approach for sending post params – Zar E Ahmer Oct 27 '16 at 07:39
  • well i have my own custom implementation of such networking , if you wish i could give you – Ahad Oct 27 '16 at 09:15
  • @AbdulAhad I used 'SyncHttpClient' instead of 'HttpUrlConnection' and it works fine, and it is surprisingly less complicated. Thanks for your help, though. :) – ashwin mahajan Oct 27 '16 at 11:07

0 Answers0