0

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

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

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

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)

MethodsgetSummoner()getSummonerInfo()是从响应字符串中解析对象的方法。我把它说出来是为了让问题更容易被理解。

谢谢!

4

0 回答 0