简体   繁体   English

将List对象传递给Android中的方法

[英]Passing a List object to a method in Android

I am trying to use the following method: 我正在尝试使用以下方法:

public static String UrlToString(String targetURL, List params) {
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(targetURL);
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("name", "xyz"));
        params.add(new BasicNameValuePair("home", "xyz"));
        post.setEntity(new UrlEncodedFormEntity(params));
        HttpResponse response = client.execute(post);
        InputStream input = response.getEntity().getContent();  
        //Get the string data from input string and return
        ...
}

But i want the List<NameValuePair> params = new ArrayList<NameValuePair>(); 但是我想要List<NameValuePair> params = new ArrayList<NameValuePair>(); to be passed with the values added from outside the method. 与从方法外部添加的值一起传递。 If I remove the three lines from there, I get an error from Eclipse saying there is an uncaught exception. 如果从此处删除三行,则会从Eclipse中收到一条错误消息,指出存在未捕获的异常。 How do I pass the list to the method? 如何将列表传递给方法?

Java is pass by value. Java是按价值传递的。

A List reference named params is passed by value into your method; 名为params的List引用按值传递给您的方法; it's not the same thing as the local variable you create here: 它与您在此处创建的局部变量不同:

List<NameValuePair> params = new ArrayList<NameValuePair>();

This code cannot possibly have the effect you want. 此代码可能无法达到您想要的效果。

Your method name should follow the Java coding conventions: it should be urlToString 您的方法名称应遵循Java编码约定:应为urlToString

Finally, if the compiler is telling you that there's an unhandled exception, please add a throws clause to your method signature or add a try/catch block if there's a meaningful handling strategy. 最后,如果编译器告诉您存在未处理的异常,请在您的方法签名中添加throws子句,如果有有意义的处理策略,请添加try / catch块。

Your signature says the method should return a String, but I don't see a return statement. 您的签名说该方法应该返回一个String,但是我看不到return语句。 This won't compile, let alone run. 这不会编译,更不用说运行了。

All those variables that are local to the static method, like the response and input stream, don't appear to do anything. 静态方法本地的所有那些变量,例如响应和输入流,似乎都没有任何作用。 They go out of scope when you exit the method call. 当您退出方法调用时,它们超出范围。 As far as I can tell this method is useless as written. 据我所知,这种方法没有用。

The uncaught exception error is coming from the HttpClient methods. 未捕获的异常错误来自HttpClient方法。 Because they are doing some I/O jobs under the covers, they can throw IOException in case of I/O failure (eg network down, server down, etc) so that you handle such case, for example to take an alternative path or to show an user friendly error message. 因为他们在进行某些I / O工作,所以在I / O发生故障(例如,网络故障,服务器故障等)的情况下,它们可能引发IOException ,以便您处理此类情况,例如采用替代路径或显示用户友好的错误消息。

You just need to wrap the code in a try-catch block or to add a throws declaration. 您只需要将代码包装在try-catch块中或添加throws声明即可。 Since you're already using Eclipse, just click the red bullet on the left hand side of the errorneous code line and explore the Eclipse-suggested solutions. 由于您已经在使用Eclipse,因此只需单击错误代码行左侧的红色项目符号,然后探索Eclipse建议的解决方案。 If you can handle the exception in a sensible manner, such as returning an alternative response, then choose for the try-catch and do the handling job inside the catch . 如果您可以以明智的方式处理异常(例如返回替代响应),请选择try-catch并在catch内执行处理工作。 But if you can't handle it in a sensible manner in this particular method, then choose for the throws clause and let the caller handle it. 但是,如果您不能通过这种特定方法以明智的方式处理它,那么请选择throws子句,然后让调用方对其进行处理。

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM