每隔一次HttpRequest中的FileNotFoundException

时间:2013-03-27 18:46:27

标签: java android network-programming

在我的Android应用程序中,我想让用户从网站获取数据(通过发送http请求)。然后将http响应发送到另一个显示数据的活动。 (我在这个示例代码中使这个更简单。实际上,数据被解析为一个对象并且对象被发送)。

这一切都适用于我的代码。但是当用户通过按返回键返回主活动并尝试另一个请求时,应用程序会在从URLConnection获取输入流时抛出java.io.FileNotFoundException。如果应用程序关闭(再次按回来),然后重新打开,则一切正常。

我很确定连接未正确关闭或类似的问题,但我无法在我的代码中找到错误。 这里有什么想法错了吗?

MainActivity.java

*no interessting code here.
it just calls the static function from MainMethods.java
when a button is pressed*

MainFunctions.java - 我想从几个类中调用方法,因此它在一个额外的类

class MainFunctions {

    public static void search(final Activity, final String searchString) {

        new AsyncTast<String, String, String>() {

            protected void onPreExecute() {
                // im opening a progress dialog for the activity here
            }

            protected String doInBackground(String... searchString) {
                try{
                    return new HttpUtils().sendRequest(searchString)
                } catch (Exception e) {
                    // print a dialog, stacktrace and stuff
                    return null;
                }
            }

            protected void onPostExecute(String result) {
                if(result != null) {
                    // dismiss the dialog etc..
                    Intent i = new Intent("packagename");
                    i.putExtra("key", result);
                    activity.startActivity(i);
                }
           }           
        }.start(searchString)
    }
}

HttpUtils.java

public class HTTPUtils {

    public String sendRequest(String data)
        throws IOException {

     String answer = "";

     URL url = new URL("http://myURL.com/" +  data);
     URLConnection conn = url.openConnection();
     conn.setReadTimeout(5000);
     BufferedReader in = new BufferedReader(new InputStreamReader(
                 conn.getInputStream()));

             String inputLine;

     while ((inputLine = in.readLine()) != null)
         answer += inputLine;

     in.close();

     return answer; 
}
}

Stacktrace显示在调用conn.getInputStream()时抛出异常。

java.io.FileNotFoundException: http://myURL.com/danijoo
at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java)
at com.elophant.utils.HTTPUtils.sendRequest(HTTPUtils.java:20)
at com.elophant.Elophant.getSummoner(Elophant.java:183)
at com.lolsummoners.datacollector.Collecter.getSummonerInfo(Collecter.java:39)
at com.lolsummoners.utils.MainFunctions$1.doInBackground(MainFunctions.java:106)
at com.lolsummoners.utils.MainFunctions$1.doInBackground(MainFunctions.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java)
at java.util.concurrent.FutureTask.run(FutureTask.java)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java)
at java.lang.Thread.run(Thread.java)

方法getSummoner()getSummonerInfo()是从响应字符串中解析对象的方法。我这样做是为了让问题更容易解决。

谢谢!

0 个答案:

没有答案
相关问题