java 返回字符串数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14160183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Returning String Array
提问by Ket
I am having trouble with returning string array on a client and server environment. The result I getting is nothing when I compiled the client application.
我在客户端和服务器环境中返回字符串数组时遇到问题。当我编译客户端应用程序时,我得到的结果是什么。
server application
服务器应用程序
public String[] getFlight() throws Exception {
AvailableFlights todayFlight = new AvailableFlights();
List<Flight> flights_today = todayFlight.getFlightDetail();
List<String> flights = new ArrayList<String>();
try {
flights_today = this.unmarshal(new File("Flights.xml"));
for (Flight flight : flights_today) {
String flightDetail = flight.getJourney()
+ " " + flight.getAirline()
+ " "+ String.valueOf(flight.getConnections())
+ " "+ String.valueOf(flight.getCost())
+ " "+ flight.getDestination()
+ " "+ flight.getOrigin()
+ " "+ String.valueOf(flight.getSeats());
flights.add(flightDetail);
System.out.println(flightDetail);
}
} catch (Exception e) {
}
return (String[]) flights.toArray();
}
client java application
客户端 Java 应用程序
import org.me.kettravel.*;
public class JavaApplication5 {
public static void main(String[] args) {
try {
System.out.println(getFlight());
} catch (Throwable ex) {
}
}
private static java.util.List<java.lang.String> getFlight() throws Exception_Exception {
org.me.kettravel.ReadFlightService service = new org.me.kettravel.ReadFlightService();
org.me.kettravel.ReadFlight port = service.getReadFlightPort();
return port.getFlight();
}
Additionally I have tried a small experiment with "hello" like below on the server app and it worked fine, so I know that the web service is working fine but I just can't seem to pass/return the flights String array to the client app.
另外,我在服务器应用程序上尝试了一个类似下面的“hello”的小实验,它运行良好,所以我知道 Web 服务运行良好,但我似乎无法将航班字符串数组传递/返回给客户端应用程序。
String i = "hello";
return i;
PS: When I try to run the server app with public static void main (String[] args) {
constructor and without return, the app printed out the arraylist perfectly from unmarshalling xml convert it to arraylist and do system.out.print
.
PS:当我尝试使用public static void main (String[] args) {
构造函数而不返回运行服务器应用程序时,该应用程序从解组 xml 中完美地打印出 arraylist 将其转换为 arraylist 并执行system.out.print
。
I would be grateful if anyone could shed some light as I am really stuck on this. Thanks.
如果有人能阐明一些观点,我将不胜感激,因为我真的坚持这一点。谢谢。
04/01/2012 (19:16)- Adjustment has been made suggested by Genzer, the client app still not getting any response from server app.
04/01/2012 (19:16)- Genzer 已提出调整建议,客户端应用程序仍未收到服务器应用程序的任何响应。
04/01/2012 (23:24)- Adjustment has been made suggested by Bohemian can be seen below, the client app is now getting an error checking javax.xml.ws.soap.SOAPFaultException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
04/01/2012 (23:24)- Bohemian 建议的调整如下所示,客户端应用程序现在进行错误检查javax.xml.ws.soap.SOAPFaultException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
public static void main(String[] args) {
try {
Object obj = getFlight();
System.out.println(obj);
} catch (Throwable ex) {
System.out.println(ex);
}
}
06/01/2013 (16:20)- I have just addressed my mistake as the XML file was empty from tests to tests, however it is now have data in the xml file. I have just created a test class to see if readFlight returns anything to a class that it's in a same project/source package. Result is a success... really running out of ideas =/ as I have tested the web service by sending a simple hello string over to client app and worked no problem.
06/01/2013 (16:20)- 我刚刚解决了我的错误,因为 XML 文件从测试到测试都是空的,但是现在它在 xml 文件中有数据。我刚刚创建了一个测试类,以查看 readFlight 是否将任何内容返回到它位于同一项目/源包中的类。结果是成功的......真的没有想法了 =/ 因为我已经通过向客户端应用程序发送一个简单的 hello 字符串来测试 Web 服务并且没有问题。
test classpublic class test {
测试类公共类测试{
public static void main(String[] args) {
readFlight rF = new readFlight();
try {
System.out.println(rF.getFlight());
} catch (Throwable ex) {
System.out.println(ex);
}
}
}
}
Output from the test class:[London to Amsterdam KLM 1 200.0 Amsterdam London 100, London to Kuala Lumper Malaysia Airline 1 750.0 Kuala Lumper London 100, London to Manchester British Airway 1 50.0 Manchester London 56]
测试类的输出:[London to Amsterdam KLM 1 200.0 Amsterdam London 100, London to Kuala Lumper Malaysia Airline 1 750.0 Kuala Lumper London 100, London to Manchester British Airway 1 50.0 Manchester London 56]
10/01/2013 (18:13)- PROBLEM SOLVED. You have to give full directory to the unmarshall file. Example: C:/Users/User/Documents/NetBeansProjects/WebService/booking.xml
10/01/2013 (18:13)- 问题已解决。您必须为解组文件提供完整目录。例子:C:/Users/User/Documents/NetBeansProjects/WebService/booking.xml
回答by NPE
The problem is that you have two different variables named flights
. You populate one and return the other.
问题是您有两个不同的变量,名为flights
. 您填充一个并返回另一个。
回答by Genzer
You could remove public static String[] flights
and modify the method like this:
您可以public static String[] flights
像这样删除和修改方法:
public List<String> getFlight() throws Exception {
Flight nextFlight = new Flight();
AvailableFlights todayFlight = new AvailableFlights();
List<Flight> flights_today = todayFlight.getFlightDetail();
// Since you you List for Flight, why not here
List<String> flights = new ArrayList<String>();
try {
flights_today = readFlight.unmarshal(new File("Flights.xml"));
for (Flight flight : flights_today) {
String flightDetail = flight.getJourney()
+ " " + flight.getAirline()
+ " "+ String.valueOf(flight.getConnections())
+ " "+ String.valueOf(flight.getCost())
+ " "+ flight.getDestination()
+ " "+ flight.getOrigin()
+ " "+ String.valueOf(flight.getSeats());
flights.add(flightDetail);
}
} catch (Exception e) {
}
return flights;
}
回答by Bohemian
You have committed a "no no", which may be hiding the problem:
你犯了一个“不不”,这可能隐藏了问题:
catch (Exception e) {
}
You should never (well, rarely) catch Exception. Especially when your catch block is empty.
你永远不应该(好吧,很少)捕获异常。尤其是当您的 catch 块为空时。
There could be an unchecked exception, like NullPointerException, being thrown within your loop, but you wouldn't know.
循环中可能会抛出一个未经检查的异常,例如 NullPointerException,但您不会知道。
Try removing the catch and leaving only soecific Exceptions (if any) that are declared to be thrown.
尝试删除捕获并只留下声明为抛出的社会异常(如果有)。
If one of the method is declared as throwing Exception, then at the very least, you should do this:
如果方法之一被声明为抛出异常,那么至少,你应该这样做:
catch (Exception e) {
System.out.println(e);
}