【问题标题】:Android retrofit get json objectAndroid改造获取json对象
【发布时间】:2014-10-16 02:57:47
【问题描述】:

我正在为 Web 服务使用改造库,该服务期望请求为 json,响应为 json。

public class GitHubClient extends Activity{

private static final String API_URL = "http://10.0.0.32/test";

static class Contributor {
    String login;
    int contributions;
}
public static class Array {
    public String id;
    public String name;
}
class Contributor1 {
    public  Array array;
}
interface Login {
    @POST("/testapp/")
    Contributor1 mu(@Body User user,Callback<Contributor1> callBack);

}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    new myasyncclass().execute();

}
public class User {
    public String mail;
    public String password;
}
public class myasyncclass extends AsyncTask<Void, Void, Void>{

    @Override
    protected Void doInBackground(Void... params) {
        // Create a very simple REST adapter which points the GitHub API endpoint.
        RestAdapter restAdapter = new RestAdapter.Builder()
        .setServer(API_URL)
        .build();
        Login login = restAdapter.create(Login.class);
        User user=new User();
        user.mail="ttete";
        user.password="tett";
        Contributor1 contributors = login.mu(user,new Callback<Contributor1>() {

            @Override
            public void failure(RetrofitError error) {                  
                System.out.println("failure, error: " + error);
            }

            @Override
            public void success(Contributor1 result, Response arg1) {
                System.out.println("success, result: " + result);

            }
        });
        return null;
    }
}

}

这是我的整个班级。但是我收到以下错误

原因:java.lang.IllegalArgumentException:方法 mu 可能只有返回类型或回调作为最后一个参数,而不是两者。

我应该怎么做才能克服这个错误?

【问题讨论】:

    标签: android web-services retrofit


    【解决方案1】:

    这个接口定义是错误的。

    interface Login {
        @POST("/testapp/")
        Contributor1 mu(@Body User user,Callback<Contributor1> callBack);
    
    }
    

    你应该修改为

    interface Login {
        @POST("/testapp/")
        Contributor1 mu(@Body User user);
    
    }
    

    由于现在它没有回调,你应该可以同步调用它。

    【讨论】:

      猜你喜欢
      • 2016-05-09
      • 1970-01-01
      • 1970-01-01
      • 2019-02-14
      • 2015-12-07
      • 2018-09-23
      • 2018-09-01
      • 2014-05-29
      • 2019-11-12
      相关资源
      最近更新 更多