如何从文件逐行写入服务器?

时间:2018-09-08 22:49:06

标签: java android

已经看到类似的问题,但没有解决方案。我的应用程序将产品信息保存到txt文件“ storage.txt”中。它临时(用于调试)将文件的内容显示为多行文本视图。我是Java的新手,并且具有其他语言的经验,我认为这使我无法在这里看到此问题。我需要做的是从“ storage.txt”中读取一行,将其上传到数据库,读取下一行,然后重复(如果有可用的网络)。如果没有可用的网络,则使用失败的上传覆盖存储。问题是,当单击按钮并且网络可用时,应用程序崩溃。

          private void readFromStorage (Context context){
            try {
                InputStream inputStream = context.openFileInput("storage.txt");
                if ( inputStream != null ) {
                    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                    String receiveString = "";
                    StringBuilder stringBuilder = new StringBuilder();
                    //StringBuilder tagBuilder = new StringBuilder();
                    List<String> tags = new ArrayList<>(); 

                    while ( (receiveString = bufferedReader.readLine()) != null ) {

                        stringBuilder.append(receiveString);

                        if(isNetworkAvailable()) {//network found, submit to site and place, do not rewrite to file
                            if (!receiveString.matches("")) {
                                sendToServer(receiveString);
                            }
                        }else{//network not found, rewrite to file
                            if (!receiveString.matches("")) {
                                tags.add(receiveString);
                            }
                        }

                        stringBuilder.append("\n");
                    }
                    inputStream.close();


                    //take new table and write to file, hopefully empty
                    for(int i = 0; i < tags.size(); i++) {
                        String str = tags.get(i).toString();
                        fileRead.append(str);
                        writeToFile(str, context);
                    }

                }
            }
            catch (FileNotFoundException e) {
                Log.e("login activity", "File not found: " + e.toString());
            } catch (IOException e) {
                Log.e("login activity", "Can not read file: " + e.toString());
            }
        }

        private void sendToServer (final String sendString){

                if (sendString != null) {

                    String[] separated = sendString.split(";");

                    caliperTXT = separated[0];
                    weightTXT = separated[1];
                    widthTXT = separated[2];
                    typeTXT = separated[3];
                    vendorTXT = separated[4];
                    poTXT = separated[5];
                    aisleTXT = separated[6];
                    plantTXT = separated[7];


                final StringRequest stringRequest = new StringRequest(Request.Method.POST, server_url,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {

                                builder.setTitle("Server Response");
                                builder.setMessage("Response: " + response);
                                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {

                                    }
                                });
                                AlertDialog alertDialog = builder.create();
                                alertDialog.show();

                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                        Toast.makeText(readStorage.this, "An error has occured", Toast.LENGTH_LONG).show();
                        error.printStackTrace();

                    }
                }) {
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String, String> params = new HashMap<String, String>();
                        params.put("caliper", caliperTXT);
                        params.put("weight", weightTXT);
                        params.put("width", widthTXT);
                        params.put("type", typeTXT);
                        params.put("vendor", vendorTXT);
                        params.put("po", poTXT);
                        params.put("aisle", aisleTXT);
                        params.put("plant", plantTXT);
                        return params;
                    }
                };

                MySingleton.mInstance(readStorage.this).addTorequestque(stringRequest);
                }
        }

0 个答案:

没有答案