E
E
Edward2016-08-26 23:57:32
Java
Edward, 2016-08-26 23:57:32

How to send a post request and parse the response?

I reviewed a lot of material, but almost everything is outdated, how to use the http client in java, I did not find a namespace for it.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Edward, 2016-08-28
@edvardpotter

public class Main2Activity extends AppCompatActivity {
    Authorization mt;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        helloTextView = (EditText)findViewById(R.id.editText);
    }

    public void onClick(View view) {
        mt = new Authorization();
        mt.execute();
    }

    class Authorization extends AsyncTask<Void, Void, Void> {

        String resultString = null;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {
            try {
                String myURL = "http://site.ru";
                String parammetrs = "login=1&password=2";
                byte[] data = null;
                InputStream is = null;

                try {
                    URL url = new URL(myURL);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("POST");
                    conn.setDoOutput(true);
                    conn.setDoInput(true);

                    conn.setRequestProperty("Content-Length", "" + Integer.toString(parammetrs.getBytes().length));
                    OutputStream os = conn.getOutputStream();
                    data = parammetrs.getBytes("UTF-8");
                    os.write(data);
                    data = null;

                    conn.connect();
                    int responseCode= conn.getResponseCode();

                    ByteArrayOutputStream baos = new ByteArrayOutputStream();

                    if (responseCode == 200) {
                        is = conn.getInputStream();

                        byte[] buffer = new byte[8192];
                        int bytesRead;
                        while ((bytesRead = is.read(buffer)) != -1) {
                            baos.write(buffer, 0, bytesRead);
                        }
                        data = baos.toByteArray();
                        resultString = new String(data, "UTF-8");
                    }                 
                } catch (IOException e) {

                    //resultString = "IOException:" + e.getMessage();
                } catch (Exception e) {

                    //resultString = "Exception:" + e.getMessage();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            if(resultString != null) {
                Toast toast = Toast.makeText(getApplicationContext(), resultString, Toast.LENGTH_SHORT);
                toast.show();
            }

        }
    }
}

I found the answer on the toaster, I was stupid for a very long time and used it in the activity stream, because I did not immediately guess that it was necessary to use AsyncTask .

D
Denis Zagaevsky, 2016-08-27
@zagayevskiy

See what needs to be posted. In my opinion, it is most convenient to take Retrofit.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question