1

I am calling php script which accesses my db from an android lass. The class is below;

public class pk_http {

    // Progress Dialog
    private ProgressDialog qDialog;

    // JSON parser class
    JSONParser jParser = new JSONParser();

    class phpCall extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... args) {
            // Building Parameters
            String url = args[0];
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url, "GET",params);



            return null;
        }
   }

Now when i call that from my calling class (the calling class does not extend Activity btw) this way;

public static ArrayList<String> getLoginTileDataArray(Context c)
{
    //CODE STUB: HTTP GET RETURNS THE FOLLOWING STRING
    String result = pk_http.phpCall.execute("http://myUrl/phpFile.php");
.
.
.

I have a error pre-compilation that says;

Non-static method 'execute(Params...) cannot be referenced from a static context.

if i remove the 'sttic' no change. Am i calling the async method correctly?

Fearghal
  • 10,569
  • 17
  • 55
  • 97
  • http://stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static?lq=1 – GriffeyDog Mar 27 '15 at 16:17

1 Answers1

1

You cannot call execute like that, it is not a static method. Use new

public static ArrayList<String> getLoginTileDataArray(Context c)
{
    //CODE STUB: HTTP GET RETURNS THE FOLLOWING STRING
    (new pk_http.phpCall()).execute("http://myUrl/phpFile.php");
.
.
.

UPDATE

Your code has errors, you missed one } in pk_http. You need search more about AsyncTask, do long-time tasks in doInBackground() and handle result to UI thread from onPostExecute()

Here is simple code:

public class pk_http {

    public void execute(String s) {
        (new phpCall()).execute(s);
    }

    // Progress Dialog
    private ProgressDialog qDialog;

    // JSON parser class
    JSONParser jParser = new JSONParser();

    class phpCall extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... args) {
            // Building Parameters
            String url = args[0];
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url, "GET", params);

            // TODO: return your result here, this will pass to onPostExecute(String)
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            // TODO: This is in UI thread, handle your result from doInBackground().
        }
    }
}

Maybe it's in MainActivity:

public static ArrayList<String> getLoginTileDataArray(Context c)
{
    //CODE STUB: HTTP GET RETURNS THE FOLLOWING STRING
    (new pk_http()).execute("http://myUrl/phpFile.php");
.
.
.
tianyu
  • 179
  • 2
  • 6
  • getting '.... .pk_http' is not an enclosing class – Fearghal Mar 27 '15 at 16:24
  • also pk_http myPhpCall = new pk_http(); String result = myPhpCall.phpCall.execute( gives 'expected class or pakage' error – Fearghal Mar 27 '15 at 16:25
  • ok no errors :) Fantastic. 1 more Q. How do i return the result in s from postExecute to calling class getLoginTileDataArray()? – Fearghal Mar 27 '15 at 16:56
  • doInBackground is asynchronous, so you need callback or send messages in postExecute – tianyu Mar 28 '15 at 01:42
  • Read more from [android asynctask](http://developer.android.com/reference/android/os/AsyncTask.html) and [this question](http://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a) – tianyu Mar 28 '15 at 01:50