0

I'm working with a simple function that I make to do an HTTP friend request that I call from another static reference. The problem is that the fragment system that google provides (Tab Navigator Activity) the prebuild code does not allow me to use a non-static method in there.

The code which I call the method

enviarsolicitud(param1,param2);

This is the code of the void that I say

       private static void enviarsolicitud(final String idadder, final String idadded) 
       {
            class EnviarSolicitudClass extends AsyncTask<String,Void,String> 
            {
                @Override
                protected void onPreExecute()
                {
                    super.onPreExecute();
                    System.out.println("enter");
                }
                @Override
                protected void onPostExecute(String s)
                {
                    super.onPostExecute(s);
                    if(s.contains("friendrequest")){
                        String[] friendrequest = s.split(":");
                        System.out.println(friendrequest[0] + " " + friendrequest[1]);
                    }
                    else if (s.contains("friendrequestcant"))
                    {

                    }
                    else
                    {

                    }
                }
                @Override
                //En segon pla
                protected String doInBackground(String... params)
                {
                    HashMap<String,String> dades = new HashMap<>();
                    dades.put("idadder",params[0]);
                    dades.put("idadded",params[1]);
                    RegisterUserClass ruc = new RegisterUserClass();
                    String resultat = ruc.sendPostRequest("http://www.myurl.net/friend.php",dades);
                    return resultat;
                }

            }
            EnviarSolicitudClass esc = new EnviarSolicitudClass();
            esc.execute(idadder,idadded);
        }

I don't know a lot about non-static and static but the problem is that google use static methods for the prebuilded activity. Do you recommend to use static for this type of voids? I always use non-static methods because static is limited a lot

Roger RV
  • 132
  • 3
  • 15
  • I am a little confused. 1. Your first example is not a method. It is a statement which creates an anonymous inner class. 2. The second example does something entirely different than the first. 3. Java does not allow class declarations inside of methods, so the second example will not even compile (assuming that the method itself is inside another class). – Code-Apprentice Jan 23 '17 at 19:09
  • @Code-Apprentice I can compile it, no errors in my code – Roger RV Jan 23 '17 at 19:21
  • @Code-Apprentice You actually can make classes in methods. Its dumb, but I've seen alot of posts here that seem to think that's how AsyncTasks should be made – OneCricketeer Jan 23 '17 at 19:23
  • 2
    Looks like I was incorrect in part 3. I've never used this feature, probably because making top-level classes is much better for organization. – Code-Apprentice Jan 23 '17 at 19:23
  • 1
    Remember that methods must be inside a class. Java does not allow free-standing lines of code, either. Your question will be much clearer if you add the details to your code snippets to make them valid Java code. – Code-Apprentice Jan 23 '17 at 19:25
  • @Code-Apprentice I think the same I do it fast but It's only just an example I was asking another thing. – Roger RV Jan 23 '17 at 19:26
  • @Code-Apprentice I'll do it, thanks. – Roger RV Jan 23 '17 at 19:27

2 Answers2

1

I don't think this is an issue of static vs non-static.

Your issue is when to write/use an AsyncTask. My suggestion would probably be switch to Volley or OkHttp if you are using HTTP methods, but if you want to continue down this path, read on.

Make an interface

public interface StringCallback {
    void onResponse(String result);
}

And add a parameter for a callback result to sendPostRequest that accept the POST response.

private void enviarsolicitud(final String idadder, final String idadded) {

    HashMap<String,String> dades = new HashMap<>();
    dades.put("idadder",params[0]);
    dades.put("idadded",params[1]);
    RegisterUserClass ruc = new RegisterUserClass();

    ruc.sendPostRequest("http://www.myurl.net/friend.php",dades,new StringCallback() {
        @Override
        public void onResponse(String response) {
            // Continue with UI logic here
        }
    });

    // Can't use HTTP response here
}

And for sendPostRequest, you use the AsyncTask, or whatever HTTP framework you want and "pass back" the response when it returns.

public void sendPostRequest(final String url, final HashMap<String, String> params, final StringCallback callback) {
    new AsyncTask<String, Void, String>() {

        @Override
        public String doInBackground(String... params) {
            // TODO: HTTP things
        }

        @Override
        protected void onPostExecute(String s)
        {
            super.onPostExecute(s);
            if (callback != null) {
                callback.onResponse(s);
            }
            return; // Done here
    }.execute(params.get("idadder"), params.get("idadded"));
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Yeah, basically, this is an adaptation from this answer http://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a – OneCricketeer Jan 23 '17 at 19:57
  • I'm not sure what youre trying to tell me, but I would suggest extract those parameters to separate variables – OneCricketeer Jan 23 '17 at 20:37
0

The solution for me is:

new HomeActivity().enviarsolicitud(param1,param2);

Last answer by @cricket_007 helps me to improve my code.

Roger RV
  • 132
  • 3
  • 15