Search code examples
androidcrudhttpconnection

Cannot resolve symbol execute, android


I am trying to set up my app to communicate with a server so I can perform profile crud. I set up a fab that when clicked will create a JSONObject and post that data to the server, all the code seems to be fine, but on "new SendUserDetails.execute("server_URL",postData.toString());" says that it cannot resolve symbol execute. Is there something I overlooked? Thanks a head of time for any help.

edit_profile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            JSONObject postData = new JSONObject();
            try{
                postData.put("Name", name.getText().toString());
                postData.put("Bio", bio.getText().toString());
                postData.put("Age", age.getText().toString());
                postData.put("Major", major.getText().toString());
                postData.put("College", college.getText().toString());
                postData.put("Random Fact", random_fact.getText().toString());

                new SendUserDetails.execute("server_URL", postData.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });

This is the code for the Http connection

private class SendUserDetails extends AsyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... params) {
        String data = "";

        HttpURLConnection httpURLConnection = null;
        try{
            httpURLConnection = (HttpURLConnection) new URL(params[0]).openConnection();
            httpURLConnection.setRequestMethod("POST");

            httpURLConnection.setDoOutput(true);

            DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
            wr.writeBytes("PostData" + params[1]);
            wr.flush();
            wr.close();

            InputStream in = httpURLConnection.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(in);

            int inputStreamData = inputStreamReader.read();
            while(inputStreamData != -1){
                char current = (char) inputStreamData;
                inputStreamData = inputStreamReader.read();
                data += current;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(httpURLConnection != null){
                httpURLConnection.disconnect();
            }
        }
        return data;
    }

    @Override
    protected void onPostExecute(String result){
        super.onPostExecute(result);
        Log.e("TAG", result);
    }
}

Solution

  • Add parens after SendUserDetails, since you're trying to call a constructor. In other words, replace new SendUserDetails.execute(...) with new SendUserDetails().execute(...).