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

时间:2021-11-27 15:39:43

Im checking how RetroFit works and tried the following:

我正在检查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);
    }
}

In Activity:

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());
            }
        });
    }

User:

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;
    }
}

Interface:

public interface TaskService {

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

the above webservice method returns the follwing:

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

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

So i am using the post method to retrieve the above data, but i keep on getting an internal server error 500 each time though when executing the webservice method from the web, data is being retrieved. so the problem is with the code above but i cant figure out what. i tried adding the .setConverter(new SimpleXmlConverter()); after looking at the following URL: Android Retrofit return status 500 internal server error but it was the same.

所以我使用post方法来检索上面的数据,但我每次都在获取内部服务器错误500但是当从web执行webservice方法时,正在检索数据。所以问题是上面的代码,但我无法弄清楚是什么。我尝试添加.setConverter(new SimpleXmlConverter());查看以下URL后:Android Retrofit返回状态500内部服务器错误,但它是相同的。

any help would be appreciated.

任何帮助,将不胜感激。

1 个解决方案

#1


Try this..... Adding a .xml will fix the problem

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

public interface TaskService {

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

And ur User class has to be in this format

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

@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;
}

EDIT 1:

@Root is a annotation and in ur case it should be @Root(name = "ROWS") which is the root element of ur XML

@Root是一个注释,在你的情况下它应该是@Root(name =“ROWS”),这是你的XML的根元素

#1


Try this..... Adding a .xml will fix the problem

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

public interface TaskService {

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

And ur User class has to be in this format

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

@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;
}

EDIT 1:

@Root is a annotation and in ur case it should be @Root(name = "ROWS") which is the root element of ur XML

@Root是一个注释,在你的情况下它应该是@Root(name =“ROWS”),这是你的XML的根元素