在同一应用中发送GET和POST请求

时间:2019-05-01 14:50:03

标签: java android http post get

我正在制作一个应用程序,可以在其中控制Arduino并从中接收数据。

我使用HttpUrlConnectionASyncTask。到目前为止,我创建的应用只能通过向其发送GET请求来通过Wi-Fi防护罩来控制Arduino。

现在,我需要从Arduino接收传感器数据并对其进行控制。但是,在代码中,我将请求方法设置为GET

是否可以在同一个类中发送GETPOST请求?我已经在下面附加了OnCreate函数。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        setTitle("Configure IP address");

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        final TextView input = (TextView) findViewById(R.id.ipTF);
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        String IP = preferences.getString("IP", "");
        input.setText(IP.replace("http://", "").replace("/", "")); //Fill in the previous set IP in the label.

        //When the ok button is clicked, show the dialog and fire of a request.
        Button ok = (Button) findViewById(R.id.okB);
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog = ProgressDialog.show(Setting.this, "Checking", "Checking the IP.", false);
                String IP = input.getText().toString();
                IP = "http://" + IP + "/";
                new RequestTask().execute(IP);//Checking if the thing is up
            }
        });

        TextView status = (TextView) findViewById(R.id.status);
        status.setText("Enter an IP address to connect to:");
    }

    //The request class.
    class RequestTask extends AsyncTask<String, String, String> {
        private String IP = "";//the variable to store the IP address to store in the preferences.
        private boolean error = false;
        public static final String REQUEST_METHOD = "GET";
        public static final int READ_TIMEOUT = 15000;
        public static final int CONNECTION_TIMEOUT = 15000;

        @Override
        protected String doInBackground(String... uri) {

            IP = uri[0];    //Store the IP
            String responseString;
            String inputLine;

            try {
                //Create new URL object and open the connection channel
                URL url = new URL(uri[0]);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                connection.setRequestMethod(REQUEST_METHOD);
                connection.setReadTimeout(READ_TIMEOUT);
                connection.setConnectTimeout(CONNECTION_TIMEOUT);

                connection.connect();


                InputStreamReader streamReader = new InputStreamReader(connection.getInputStream());

                BufferedReader reader = new BufferedReader(streamReader);
                StringBuilder stringBuilder = new StringBuilder();

                while((inputLine = reader.readLine()) != null){
                    stringBuilder.append(inputLine);
                }

                reader.close();
                streamReader.close();

                responseString = stringBuilder.toString();
            }
            //Oops, something  went wrong.
            catch (IOException e) {
                e.printStackTrace();
                responseString = null;
            }

            return responseString;

}

0 个答案:

没有答案