我在Spring Framework中开发了一个Server。
当我尝试通过粘贴网址的浏览器网站控制响应时,似乎List(Response.java' s字段)具有正确的大小(与方法getFourMyObject()返回的列表相同),我还尝试在服务器的控制台上打印大小,它不等于1.
但是当我从我的Android应用程序获得响应时,它不会识别另一个MyObject而是第一个。
因此,在控制器的服务器中,响应列表的大小与从客户端接收的大小不同。
我的服务器代码:
@RequestMapping("/")
public class RouteController extends RestGatewaySupport {
@RequestMapping(value = "/",method = RequestMethod.GET)
public String index (){
return "index";
}
@RequestMapping(value = "/objectize",method = RequestMethod.GET)
public @ResponseBody Response objectize(@RequestParam(value="time", defaultValue="-1") String time,
@RequestParam(value="id", defaultValue="-1") String id,
@RequestParam(value="ratio", defaultValue="-1") String destination) {
return new Response().addAllMyObjects(MyObject.getFourMyObject());
}
Response.java:
public class Response implements Serializable{
public final List<MyObject> response;
public Response () {
this .response = new ArrayList<>();
}
public Response addmObect(MyObject mObject){
this .response.add(mObject);
return this;
}
public Response addAllMyObjects (List<MyObject> mObjects){
this .response.addAll(mObjects);
return this;
}
}
Android应用的请求方法:
protected List<Route> doInBackground(String... params) {
String link = params[0];
URL url;
try {
url = new URL(link);
InputStream is = url.openConnection().getInputStream();
StringBuilder jsonData = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = reader.readLine()) != null)
jsonData.append(line).append("\n");
JSONObject jsonObject = new JSONObject(jsonData.toString());
JSONArray jsonMyObjects = jsonObject.getJSONArray("response");
Log.i("logger","jsonMyObjects list's size = "+jsonMyObjects.length());
return jsonMyObjects;
} catch (IOException | JSONException e) {
e.printStackTrace();
}
return null;
}
有人能帮助我吗? 这是一个错误的解析问题?