0

this is the code

protected Void doInBackground(String... params) {
   String reg_url = "http://10.0.2.2/";
    String method = params [0];
    if (method.equals("register") ){
        String first_name = params [1];
        String last_name = params [2];
        String address = params [3];
        String email = params [4];
        String password = params [5];

        try {
            URL url = new URL(reg_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            HttpURLConnection.setRequestMethod("POST");
            HttpURLConnection.setDoOutput(True);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    return null;
}

and i am getting the error on these two lines

HttpURLConnection.setRequestMethod("POST");
            HttpURLConnection.setDoOutput(True);

on the set.RequestMethod("POST") and "setDoOutput(true); error says non static method cannot be referenced from a static context. it must be silly mistake but i just can't figure it out so can anybody help me with this please?

uma nab
  • 45
  • 1
  • 3
  • 8
  • thank you all three members who helped me with my problem. you're the best – uma nab Dec 13 '15 at 12:00
  • In the future please search for similar questions. This one is asked at least 5 to 10 times a week, and I don't think that the site will benefit from the addition of yet one more. For example: [simple search on error message](http://stackoverflow.com/search?q=%5Bjava%5D+non+static+method+cannot+be+referenced+from+a+static). – Hovercraft Full Of Eels Dec 13 '15 at 12:33

3 Answers3

2

Use the instance you obtained in the call to openConnection :

        HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setDoOutput(true);

Note that Java is case sensitive. HttpURLConnection is the class name. httpURLConnection is a variable referring to an instance of the class.

Eran
  • 387,369
  • 54
  • 702
  • 768
0
HttpURLConnection.setRequestMethod("POST");
HttpURLConnection.setDoOutput(True);

you are accessing non static methods with static method syntax

use the reference httpURLConnection too access the methods

httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
Ramanlfc
  • 8,283
  • 1
  • 18
  • 24
0

You have to invoke setRequestMethod and setDoOutput methods on the URLConnection instance.

protected Void doInBackground(String... params) {
   String reg_url = "http://10.0.2.2/";
    String method = params [0];
    if (method.equals("register") ){
        String first_name = params [1];
        String last_name = params [2];
        String address = params [3];
        String email = params [4];
        String password = params [5];

        try {
            URL url = new URL(reg_url);
            HttpURLConnection con = (HttpURLConnection)url.openConnection();
            con.setRequestMethod("POST");
            con.setDoOutput(True);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    return null;
}
11thdimension
  • 10,333
  • 4
  • 33
  • 71