文件结束例外

时间:2016-03-12 12:18:08

标签: android mysql node.js runnable eofexception

美好的一天。

我的NodeJS服务器出了问题。

我有一个Android应用程序,每隔30秒通过HttpUrlConnection从MySQL数据库获取信息。该应用程序和我的服务器工作正常。但大约(不确定)10分钟或20分钟后?我的服务器什么也没做,在我的Logcat中它说我的应用程序抛出了EOFException

有人可以帮我解决这个问题吗?

此图片显示应用正常运行

this image shows the app is working fine

和我的服务器

and my server

但过了一段时间

but after sometime

服务器成为了这个

server became this

这是我从mysql nodejs server获取信息的代码

    protected String doInBackground(String... arg0) {

    HttpURLConnection connection = null;
    HttpURLConnection connection1 = null;
    String json = (String) arg0[0];
    System.setProperty("http.keepAlive", "false");

    try {

        URL u = new URL("http://"+ MainActivity.ipadd +"/getmessage");
        connection = (HttpURLConnection) u.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Connection", "close");
        connection.setRequestProperty("Content-Type","application/json"); 

        //connection.setConnectTimeout(10000);
        //connection.setReadTimeout(15000);

        if (json != null) {
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);
            Log.i("message", "To send : " + json);
            byte[] outputInBytes = json.getBytes("UTF-8");
            OutputStream os = connection.getOutputStream();
            os.write( outputInBytes );    
            os.close();
            os.flush();
        }

        //Connect to the server
        connection.connect();

        int status = connection.getResponseCode();
        String staRes = connection.getResponseMessage().toString();
        Log.i("HTTP Client", "HTTP status code : " + status  + " " + staRes);
        switch (status) {
            case 200:
            case 201:
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                sb = new StringBuilder();
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                bufferedReader.close();
                Log.i("HTTP Client", "Response : " + sb.toString());
                //return received string
                holds.setRespone(sb.toString());
                jo = new JSONObject(sb.toString());
                jof = new JSONObject();
                String tmp = jo.optString("mess");
                String tmpp = jo.optString("cp");
                String reff = jo.optString("ref");
                boolean tmppp = false;
                //jof.put("ref", jo.opt("ref"));
                if(tmp.contains("err_code_01")){
                    tmppp = sendSMSMessage(tmpp, "Your MESSAGE was rejected. It contains wrong format.\nTrace Number:" + reff + "\nDo not reply.");
                    Log.v("sent message", "Your MESSAGE was rejected. It contains wrong format.");
                } else if(tmp.contains("err_code_03")){
                    tmppp = sendSMSMessage(tmpp, "Your MESSAGE was rejected. It contains wrong position format.\nTrace Number:" + reff + "\nDo not reply.");
                    Log.v("sent message", "Your MESSAGE was rejected. It contains wrong position format.");
                } else if(tmp.contains("err_code_04")){
                    tmppp = sendSMSMessage(tmpp, "Your MESSAGE was rejected. It contains wrong candidate-position format.\nTrace Number:" + reff + "\nDo not reply.");
                    Log.v("sent message", "Your MESSAGE was rejected. It contains wrong candidate-position format.");
                } else if(tmp.contains("err_code_05")){
                    tmppp = sendSMSMessage(tmpp, "Your MESSAGE was rejected. It contains wrong vote count format.\nTrace Number:" + reff + "\nDo not reply.");
                    Log.v("sent message", "Your MESSAGE was rejected. It contains wrong vote count format.");
                } else if(tmp.contains("success")){
                    tmppp = sendSMSMessage(tmpp, "Your MESSAGE was accepted.\nTrace Number:" + reff + "\nDo not reply.");
                    Log.v("sent message", "Your MESSAGE was accepted.");
                }
                return sb.toString();
        }

    } catch (MalformedURLException ex) {
        Log.e("HTTP Client", "Error in HTTP Connection(Malformed URL) " + ex);
    } catch (IOException ex) {
        Log.e("HTTP Client", "Error in HTTP Connection(IO Exception) " + ex);
    } catch (Exception ex) {
        Log.e("HTTP Client", "Error in HTTP Connection(Exception) " + ex);
    } finally {
        //if (connection != null) {
            //try {
                connection.disconnect();
            //} catch (Exception ex) {
               // Log.e("HTTP Client", "Error in HTTP Connection " + ex.toString());
            //}
        //}
    }

    return null;
}

以及从MainActivity.class调用

final Handler handlerr = new Handler(); 
    runnableGet = new Runnable(){
        @Override
        public void run() {
            try{
                gett = new GetMessageFromServer();
                gett.execute(tmpp.toString());
            }catch (Exception e){
                //err here
                Log.v("err", "runnable: " + e.toString());
            } finally{
                handlerr.postDelayed(runnableGet, 30000);
            }
        }
    };
    handlerr.postDelayed(runnableGet, 30000);

0 个答案:

没有答案