我是安卓开发的新手,尝试使用OkHttp在我的安卓应用程序上显示数据,我引用了几个例子,然后将它们组装到下面的类中,但它没有获取任何数据,我希望从OkHttp类中获取数据,然后将其发送到API类,我可以问一下它出了什么问题吗?
OkHttp类:
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类:
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);
}
});发布于 2017-07-21 22:42:51
您不能创建顶级静态类(OkHttpPost);这是编译器试图告诉您的。另请看答案here,了解为什么会出现这种情况。要点是:
静态可以归结为类的一个实例可以独立存在。或者,反过来:没有外部类的实例,非静态内部类(=实例内部类)就不能存在。因为顶级类没有外部类,所以它只能是静态的。
因为所有顶级类都是静态的,所以在顶级类定义中使用static关键字是没有意义的。
https://stackoverflow.com/questions/45239880
复制相似问题