此方法必须返回InputStream类型的结果

时间:2013-09-03 14:46:31

标签: java android methods inputstream

我收到错误,说明This method must return a result of type InputStream

但是我已经将InputStream的return语句添加到source方法中。所以我不确定为什么会出现这个问题。 (当我尝试在我当前的源中集成以前使用的发布进度方法时,问题就开始了。)

来源:

        /*
         * Sends a query to server and gets back the parsed results in a bundle
         * urlQueryString - URL for calling the webservice
         */

        protected synchronized InputStream getQueryResults(String urlQueryString)
                throws IOException, SAXException, SSLException,
                SocketTimeoutException, Exception {

            try {
                // HttpsURLConnection https = null;
                String uri = urlQueryString;

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


                BasicNameValuePair mdn1, mdn2,id1,id2;
                if (MDN.equals("")) {
                    mdn1 = new BasicNameValuePair("mdn1", null);
                    mdn2 = new BasicNameValuePair("mdn2", null);
                } else {
                    mdn1 = new BasicNameValuePair("mdn1", mdN1.toString());
                    mdn2 = new BasicNameValuePair("mdn2", mdN2.toString());

                }

                BasicNameValuePair car = new BasicNameValuePair("car", caR);
                if (ICCID.equals("")) {
                     id1 = new BasicNameValuePair("id1", null);
                     id2 = new BasicNameValuePair("id2", null);
                } else {
                     id1 = new BasicNameValuePair("id1",
                            iD1.toString());
                     id2 = new BasicNameValuePair("id2",
                            iD2.toString());
                }


                nameValuePairs.add(mdn1);
                nameValuePairs.add(mdn2);
                nameValuePairs.add(car);
                nameValuePairs.add(id1);
                nameValuePairs.add(id2);

                UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(
                        nameValuePairs, "ISO-8859-1");
                KeyStore trustStore = KeyStore.getInstance(KeyStore
                        .getDefaultType());
                trustStore.load(null, null);

                SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
                sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

                HttpParams params = new BasicHttpParams();
                HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
                HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

                SchemeRegistry registry = new SchemeRegistry();
                registry.register(new Scheme("http", PlainSocketFactory
                        .getSocketFactory(), 80));
                registry.register(new Scheme("https", sf, 443));

                ClientConnectionManager ccm = new ThreadSafeClientConnManager(
                        params, registry);

                HttpClient httpClient = new DefaultHttpClient(ccm, params);
                params = httpClient.getParams();
                HttpClientParams.setRedirecting(params, true);

                HttpPost httpPost = new HttpPost(uri);
                httpPost.addHeader("Authorization",
                        getB64Auth("nmundru", "abc123"));

                httpPost.setHeader("Content-Type", "text/plain; charset=utf-8");

                Log.v("httpPost", httpPost.toString());

                httpPost.setEntity(urlEncodedFormEntity);
                HttpResponse httpResponse = httpClient.execute(httpPost);
                System.out.println("response...." + httpResponse.toString());
                Log.v("response...", httpResponse.toString());

                stream = httpResponse.getEntity().getContent();

                // save the InputStream in a file

                try {

                    FileOutputStream fOut = openFileOutput("settings.xml",
                            Context.MODE_WORLD_READABLE);

                    DataInputStream in = new DataInputStream(stream);
                    BufferedReader br = new BufferedReader(
                            new InputStreamReader(in));
                    String strLine;
                    while ((strLine = br.readLine()) != null) {
                         System.out.println(strLine); //to print the response
                        // in logcat
                        fOut.write(strLine.getBytes());

                    }
                    fOut.close();

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                fis = openFileInput("settings.xml");

            } catch (Exception e) {
                Log.e(LOG_TAG, e.toString());

                // e.printStackTrace();
                tryagain();

            } finally {
                // https.disconnect();
            }

            publishProgress(R.drawable.loading_full,
                    R.drawable.loading_empty, R.drawable.loading_empty,
                    R.drawable.loading_empty, R.drawable.loading_empty);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block

            }
            publishProgress(R.drawable.loading_full,
                    R.drawable.loading_full, R.drawable.loading_empty,
                    R.drawable.loading_empty, R.drawable.loading_empty);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block

            }
            publishProgress(R.drawable.loading_full,
                    R.drawable.loading_full, R.drawable.loading_full,
                    R.drawable.loading_empty, R.drawable.loading_empty);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block

            }
            publishProgress(R.drawable.loading_full,
                    R.drawable.loading_full, R.drawable.loading_full,
                    R.drawable.loading_full, R.drawable.loading_empty);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block

            }
            publishProgress(R.drawable.loading_full,
                    R.drawable.loading_full, R.drawable.loading_full,
                    R.drawable.loading_full, R.drawable.loading_full);

            // Sleep for 1/2 second
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block


            return stream;
        }
    }

2 个答案:

答案 0 :(得分:4)

目前,如果最终try块抛出InterruptedException,您只返回流,因为您的return语句位于catch块中。我想你的意思是稍后将return语句放在一个大括号 - 之后的<{em} catch块。否则,try / catch语句的结尾是可以访问的(因为try块可能不会抛出异常),这意味着你可以在不返回任何内容的情况下到达方法的末尾 - 这就是编译器抱怨的内容。 / p>

所以不要这样:

protected synchronized InputStream getQueryResults(...) {
    ...
    // Indentation fixed for clarity
    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
       // TODO Auto-generated catch block
       return stream;
    }
}

你想:

protected synchronized InputStream getQueryResults(...) {
    ...
    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
       // TODO Auto-generated catch block
    }
    return stream;
}

如果你已经适当地格式化了你的代码 - 你的IDE会乐意为你做的 - 你可以为自己更清楚地看到这一点,我怀疑。

答案 1 :(得分:3)

而不是:

  return stream;
    }
}  

这样做:

  }
return stream;        
}