【问题标题】:The type Enum is not generic; it cannot be parameterized with arguments <RestClient.RequestMethod>Enum 类型不是通用的;它不能用参数 <RestClient.RequestMethod> 参数化
【发布时间】:2010-11-19 14:21:37
【问题描述】:
The type Enum is not generic; it cannot be parameterized with arguments <RestClient.RequestMethod>

我在下面的代码中有这个错误..

package ayanoo.utility;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.Vector;

    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.client.methods.HttpUriRequest;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.protocol.HTTP;

    import android.util.Log;

    public class  RestClient {

     public enum RequestMethod
     {
     GET,
     POST
     }

        private Vector <NameValuePair> params;

        private String url;

        private int responseCode;
        private String message;

        private String response;

        public String getResponse() {
            return response;
        }

        public String getErrorMessage() {
            return message;
        }

        public int getResponseCode() {
            return responseCode;
        }

        public RestClient(String url)
        {
            this.url = url;
            params = new Vector<NameValuePair>();
        }

        public void AddParam(String name, String value)
        {
            params.add(new BasicNameValuePair(name, value));
        }

        public void Execute(RequestMethod method) throws IOException
        {
            switch(method) {
                case GET:
                {
                    //add parameters
                    String combinedParams = "";
                    if(!params.isEmpty()){
                        combinedParams += "/";
                        for(NameValuePair p : params)
                        {
                            //String paramString = p.getName() + "=" + p.getValue();
                         String paramString = p.getValue();
                            if(combinedParams.length() > 1)
                            {
                                combinedParams  +=  "&" + paramString;
                            }
                            else
                            {
                                combinedParams += paramString;
                            }
                        }
                    }

                    Log.d("URL See:",url + combinedParams);
                    URL urlObject = new URL(url + combinedParams);
                    //URL urlObject = new URL("http://www.aydeena.com/Services/Search.svc/JSON/SearchByText/1");

                    executeRequest(urlObject);
                    break;
                }
                case POST:
                {
                    HttpPost request = new HttpPost(url);

                    //add headers

                    if(!params.isEmpty()){
                        request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
                    }

                    executeRequest(request, url);
                    break;
                }
            }
        }

        private void executeRequest(URL urlObject) throws IOException{
         HttpURLConnection con = null;
         con = (HttpURLConnection) urlObject.openConnection();
            con.setReadTimeout(10000 /* milliseconds */);
            con.setConnectTimeout(15000 /* milliseconds */);
            con.setRequestMethod("GET");
            //con.addRequestProperty("Referer",
              //    "http://www.pragprog.com/titles/eband/hello-android");
            con.setDoInput(true);

            // Start the query
            con.connect();
            response = convertStreamToString(con.getInputStream());
            Log.d("Response:", response);
        }

        private void executeRequest(HttpUriRequest request, String url)
        {
            HttpClient client = new DefaultHttpClient();
            Log.d("Test URL:", url);

            HttpResponse httpResponse;

            try {
                httpResponse = client.execute(request);
                responseCode = httpResponse.getStatusLine().getStatusCode();
                message = httpResponse.getStatusLine().getReasonPhrase();

                HttpEntity entity = httpResponse.getEntity();

                if (entity != null) {

                    InputStream instream = entity.getContent();
                    response = convertStreamToString(instream);

                    // Closing the input stream will trigger connection release
                    instream.close();
                }

            } catch (ClientProtocolException e)  {
                client.getConnectionManager().shutdown();
                e.printStackTrace();
            } catch (IOException e) {
                client.getConnectionManager().shutdown();
                e.printStackTrace();
            }
        }

        private static String convertStreamToString(InputStream is) {

            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();

            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return sb.toString();
        }
    }

有什么问题??? !

【问题讨论】:

    标签: java android enums


    【解决方案1】:

    我遇到了同样的问题,原来是因为标准库不在项目的eclipse类路径中。只需进入 Build Path -> Add Libraries 并添加 JRE System Library

    【讨论】:

    • 谢谢 - 这也是我的问题。 JRE 系统库已定义,但指向无效路径。
    【解决方案2】:

    您确定 Java 编译器设置为 1.5(android 的默认值)或更高版本吗?如果您使用的是 Eclipse,则可以从首选项中看到。

    【讨论】:

    • 这很奇怪。你用的是什么平台?通过注释掉枚举并使用 Java 1.5 功能(例如,可变参数方法:private myMethod(String... args))来做一个简单的实验。
    • 不不,解决了..你知道怎么做吗??我已经运行了另一个项目..并且错误已解决
    • 如果您使用的是 Java 1.4 编译器,我只能解释错误。
    • 也许您可以将解决方案作为答案发布并接受。
    【解决方案3】:

    我遇到了同样的问题。

    我的项目中只有一个错误是“不是通用的”。

    在我注释掉 Enum 代码后,我发现了更多错误。

    似乎有某种阻碍。只有在修复其他错误然后删除 cmets 后它才起作用。

    【讨论】:

      【解决方案4】:

      是的,我还看到了这个错误消息,表明之前运行良好的项目。

      我检查了编译器版本(我使用的是 1.6)以及系统库(它已经在使用),但无济于事。

      最后我只是关闭了项目然后重新打开它,然后问题就消失了。对我来说听起来像是一个 Eclipse 错误。

      【讨论】:

        猜你喜欢
        • 2016-06-01
        • 2015-06-15
        • 2013-04-30
        • 2019-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多