0

I'm new to Android development and try to use OkHttp making a POST request showing the data upon my Android APP, I referred to several examples then assemble them to the classes beneath, yet it didn't acquire any data, I expect to acquire the data from the OkHttp class, afterward send it to the API class, may I ask where it gone wrong and how to adjust it?

the OkHttp class:

public static class OkHttpPost {
    OkHttpClient client = new OkHttpClient();

    RequestBody requestBody = new MultipartBody.Builder()
        .setType(MultipartBody.FORM)
        .build();

    @TargetApi(Build.VERSION_CODES.KITKAT)
    String run(String url) throws IOException {

        Request request = new Request.Builder()

                .method("POST", requestBody.create(null, new byte[0]))
                .url("124.173.120.222")
                .post( requestBody )
                .build();

        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }

    public static void main(String[] args) throws IOException {
        OkHttpPost Post = new OkHttpPost();
        String response = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            response = Post.run("124.173.120.222");
        }
        System.out.println(response);
    }}

API class:

ConnectAPI.A110(context, Scode, Manual, new ZooCallback(){
        @Override

        public void onSuccess(JSONObject response){
            super.onSuccess(response);


            try{
                new OkHttpPost();
                open.setText(response.getString("Open"));
                high.setText(response.getString("High"));
                low.setText(response.getString("Low"));
            }
            catch(JSONException e){
                Log.d("A110 gone wrong", e.getMessage());

            }

        }
        public void onFail(String title, String errorMessage){
            super.onFail(title, errorMessage);
            Log.d(TAG, errorMessage);
        }
    });
Meowmicanfly
  • 49
  • 1
  • 7

1 Answers1

0

You can't create a top level static class (OkHttpPost); that's what the compiler is trying to tell you. Also have a look at the answer here as to why this is the case. The gist is:

What the static boils down to is that an instance of the class can stand on its own. Or, the other way around: a non-static inner class (= instance inner class) cannot exist without an instance of the outer class. Since a top-level class does not have an outer class, it can't be anything but static.

Because all top-level classes are static, having the static keyword in a top-level class definition is pointless.

Community
  • 1
  • 1
phatnhse
  • 3,870
  • 2
  • 20
  • 29