首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将后台的POST数据传输到Android?

如何将后台的POST数据传输到Android?
EN

Stack Overflow用户
提问于 2017-07-21 22:11:28
回答 1查看 138关注 0票数 0

我是安卓开发的新手,尝试使用OkHttp在我的安卓应用程序上显示数据,我引用了几个例子,然后将它们组装到下面的类中,但它没有获取任何数据,我希望从OkHttp类中获取数据,然后将其发送到API类,我可以问一下它出了什么问题吗?

OkHttp类:

代码语言:javascript
复制
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类:

代码语言:javascript
复制
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);
        }
    });
EN

回答 1

Stack Overflow用户

发布于 2017-07-21 22:42:51

您不能创建顶级静态类(OkHttpPost);这是编译器试图告诉您的。另请看答案here,了解为什么会出现这种情况。要点是:

静态可以归结为类的一个实例可以独立存在。或者,反过来:没有外部类的实例,非静态内部类(=实例内部类)就不能存在。因为顶级类没有外部类,所以它只能是静态的。

因为所有顶级类都是静态的,所以在顶级类定义中使用static关键字是没有意义的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45239880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档