获取RetrofitError:500内部服务器错误

时间:2015-05-04 09:17:18

标签: java android retrofit

我正在检查RetroFit的工作原理并尝试以下方法:

RestClient:

public class RestClient {

    private static TaskService REST_CLIENT;
    private static String ROOT =
            "URL";
    static {
        setupRestClient();
    }

    private RestClient() {}

    public static TaskService get() {
        return REST_CLIENT;
    }

    private static void setupRestClient() {
        OkHttpClient OkHttpClient=new OkHttpClient();

        RestAdapter.Builder builder = new RestAdapter.Builder()
                .setEndpoint(ROOT)
                .setClient(new OkClient(new OkHttpClient()))
        .setConverter(new SimpleXmlConverter());
        builder.setLogLevel(RestAdapter.LogLevel.FULL);
        RestAdapter restAdapter = builder.build();
        REST_CLIENT = restAdapter.create(TaskService.class);
    }
}

活动:

public void testMethod()
    {
        User user=new User("58","345435","4","","0");
        RestClient.get().getWeather(user,new Callback<String>() {

            @Override
            public void success(String s, Response response) {
                Log.d("testret", "success: " + s);
                Log.d("testret", "successu: " + response.getUrl());
                Log.d("testret", "successs: " + response.getStatus());
            }
            @Override
            public void failure(RetrofitError error)
            {
                Log.d("testret", "errore: " + error);
                Log.d("testret", "erroru: " + error.getUrl());
            }
        });
    }

用户:

public class User {



    String FTID,UUID,TYPE,DateTimeStamp,SDFVersionNb;

    public User(String FTID,String UUID,String TYPE,String DateTimeStamp,String SDFVersionNb)
    {
        this.DateTimeStamp=DateTimeStamp;
        this.FTID=FTID;
        this.UUID=UUID;
        this.SDFVersionNb=SDFVersionNb;
        this.TYPE=TYPE;
    }
}

界面:

public interface TaskService {

    @POST("/GetBulkQueryUpdates")
    void getWeather(@Body User user,
                    Callback<String> callback);
}

上面的webservice方法返回以下内容:

<ROWS>
<ROW>
<GroupBy>3</GroupBy>
<QUERY>
 Query Statement
</QUERY>
</ROW>
</ROWS>

所以我使用post方法来检索上面的数据,但是我每次都在获取内部服务器错误500,但是当从web执行webservice方法时,正在检索数据。所以问题是上面的代码,但我无法弄清楚是什么。我尝试添加.setConverter(new SimpleXmlConverter());看了以下网址后: Android Retrofit return status 500 internal server error 但它是一样的。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

试试这个......添加.xml将解决问题

public interface TaskService {

@POST("/GetBulkQueryUpdates.xml")
void getWeather(@Body User user,
                Callback<String> callback);
}

你的User类必须采用这种格式

@Root(name = "breakfast_menu")
public class BreakFastMenu
{
@ElementList(inline = true)
List<Food> foodList;
}

@Root(name="food")
public class Food
{
@Element(name = "name")
String name;

@Element(name = "price")
String price;

@Element(name = "description")
String description;

@Element(name = "calories")
String calories;
}

编辑1:

@Root是一个注释,在个案中它应该是@Root(name = "ROWS"),这是你的XML的根元素