I'm just starting out with Retrofit, I'm trying to get data from a server via a @POST request but I keep returning a 500 internal server error.
My aim is to send a user name through request and the server should return a JSON string containing a list of data for given user.
I'm sure its something simple as I'm fairly fresh to using Retrofit, some code below:
my serviceGenerator(uses authentication):
private static final String URL = "http://IP address/ProjectZWS/projectzWebService.asmx";
private static RestAdapter.Builder builder = new RestAdapter.Builder()
.setEndpoint(URL)
.setClient(new OkClient(new OkHttpClient()));
public static <S> S createService(Class<S> serviceClass, String username, String password) {
if (username != null && password != null) {
// concatenate username and password with colon for authentication
String credentials = username + ":" + password;
// create Base64 encodet string
final String basic =
"Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
builder.setRequestInterceptor(new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) {
request.addHeader("Authorization", basic);
request.addHeader("Accept", "application/x-www-form-urlencoded");
}
});
}
RestAdapter adapter = builder.build();
return adapter.create(serviceClass);
My service class:
@FormUrlEncoded
@POST("/GetData")
public void GetData(@FIELD String user, Callback<List<Data>> callback);
And my main activity code:
InstituteService service = RestService.createService(InstituteService.class, "user", "pass");
service.GetData("aUser","",new Callback <List<Data>>() {
@Override
public void success(List<Data> data, Response response) {
Toast.makeText(ctx, "success!!!!", Toast.LENGTH_SHORT).show();
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(ctx, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
Server log:
2015-10-31 10:47:02 192.168.1.14 POST /ProjectZWS/projectzWebService.asmx/GetReviews - 80 jeff 176.61.63.95 okhttp/2.5.0 - 500 0 0 1328
Request information:
Request URL: http://32.17.47.56/ProjectZWS/projectzWebService.asmx/
Request path: /ProjectZWS/projectzWebService.asmx/
User host address: 176.61.65.876
User: PC\the pc
Is authenticated: True
Authentication Type: Basic
Thread account name: IIS APPPOOL\ASP.NET v4.0 Classic
Thread information:
Thread ID: 10
Thread account name: IIS APPPOOL\ASP.NET v4.0 Classic
Is impersonating: False
Stack trace: at System.Web.Script.Services.RestHandler.CreateHandler(HttpContext context)
at System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated)
at System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig)
at System.Web.HttpApplication.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Process information:
Process ID: 3288
Process name: w3wp.exe
Account name: IIS APPPOOL\ASP.NET v4.0 Classic
Exception information:
Exception type: InvalidOperationException
Exception message: Invalid web service call, expected path info of /js/<Method>.
at System.Web.Script.Services.RestHandler.CreateHandler(HttpContext context)
at System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated)
at System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig)
at System.Web.HttpApplication.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
The server is ASMX, and when the server is sent the GetData method it returns the JSON string with requested data (I have a server guy working backend and he seems happy server is responding fine to the GetData(aUser) command?). The Server is seeing something but it seems like its rubbish?
Any help appreciated.
答案 0 :(得分:0)
问题已解决,问题出现在服务类中,添加了@FIELD注释&amp;在方法调用中添加了@FIELD注释和键值对,它可以处理。在上面的初始帖子中编辑的全功能代码,下面是最终修复代码:
@FormUrlEncoded
@POST("/GetData")
public void GetData(@FIELD String user, Callback<List<Data>> callback);