1

我需要你的帮助来解决我遇到的两个错误

  1. 创建一个线程,我正在创建一个文件
  2. 在文件内容之后,执行 AsyncTask 以将文件发送到服务器(multipart/form-data)

这就是第一部分的样子:

public void startResultTransfer(final int timestamp, final int duration, final String correction, final float textSize, final int age, final int switch_count, final Activity activity){

    synchronized(DataTransmission.class){ 

        new Thread() {
            public void run() {
                FileWriter fw = null;
                //1.Check if file exists
                File file = new File(FILE_PATH);
                if(!file.exists()){
                    //File does not exists, when we have to generate the head-line
                    try {
                        fw = new FileWriter(FILE_PATH);
                        fw.append("timestamp\tduration\tcorrection\ttext_size\tage\tswitch_count"); //Headline
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                //2. Write Result
                try {
                    if(fw == null)
                        fw = new FileWriter(FILE_PATH);
                    fw.append("\n"+String.valueOf(timestamp)+"\t");
                    fw.append(""+String.valueOf(duration)+"\t");
                    fw.append(""+correction+"\t");
                    fw.append(""+String.valueOf(textSize)+"\t");
                    fw.append(""+String.valueOf(age)+"\t");
                    fw.append(""+String.valueOf(switch_count)+"\t");
                    fw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                //3. File Transfer
                if(isOnline(activity))
                    transferFileToServer(activity);
            }
        }.start();

    }
}

函数“transferFileToServer”如下所示:

public synchronized void transferFileToServer(Activity activity){
    String id = id(activity);
    File file = new File(FILE_PATH);

    if(id != null && file.exists()){
        final String url = URL+id;
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                TransmissionTask task = new TransmissionTask();
                task.execute(url);
            }
        });

    }

}

现在,我收到带有解释性消息的“ExceptionInInitializerError”

由 java.lang.RuntimeException 导致无法在未调用 Looper.prepare() 的线程内创建处理程序”

在“activity.runOnUiThread”行。

在第一个函数中,我需要在一些预先设置后调用“transferFileToServer”。但是该函数也应该从第一个函数中单独调用。

我是否应该在线程结束时实现一个 MessageHandler 来执行 AsyncTask? http://developer.android.com/reference/android/os/Looper.html

或者我应该将“transferFileToServer”函数中的“AsyncTask”更改为线程,因为我不做任何 UI 操作?

编辑:从异步任务开始的方法

class TransmissionTask extends AsyncTask<String, Void, String> {

    public TransmissionTask() {

    }

    @Override
    protected String doInBackground(String... params) {
        synchronized(DataTransmission.class){
            try {

                HttpURLConnection urlConn;
                java.net.URL mUrl = new java.net.URL(params[0]);
                urlConn = (HttpURLConnection) mUrl.openConnection();
                urlConn.setDoOutput(true);
                urlConn.setRequestMethod("POST");

                String boundary = "---------------------------14737809831466499882746641449";
                String contentType = "multipart/form-data; boundary="+boundary;
                urlConn.addRequestProperty("Content-Type", contentType);

                DataOutputStream request = new DataOutputStream(urlConn.getOutputStream());
                request.writeBytes("\r\n--"+boundary+"\r\n");
                request.writeBytes("Content-Disposition: form-data; name=\"userfile\"; filename=\""+FILE_NAME+"\"\r\n");
                request.writeBytes("Content-Type: application/octet-stream\r\n\r\n");

                File myFile = new File(FILE_PATH);
                int size = (int) myFile.length();
                byte[] bytes = new byte[size];
                try {
                    BufferedInputStream buf = new BufferedInputStream(new FileInputStream(myFile));
                    buf.read(bytes, 0, bytes.length);
                    buf.close();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 
                request.write(bytes);
                request.writeBytes("\r\n--"+boundary+"--\r\n");

                request.flush();
                request.close();

                InputStream responseStream = new BufferedInputStream(urlConn.getInputStream());

                BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream));
                String line = "";
                StringBuilder stringBuilder = new StringBuilder();
                while ((line = responseStreamReader.readLine()) != null)
                {
                    stringBuilder.append(line).append("\n");
                }
                responseStreamReader.close();

                String response = stringBuilder.toString();
                responseStream.close();
                urlConn.disconnect();

                return response;

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        if(result != null){
            if(result.toLowerCase().contains("erfolgreich")){
            //If successfull delete File
            File file = new File(FILE_PATH);
            file.delete();
            }
        }   

    }

}
4

1 回答 1

0

删除runOnUiThread

public synchronized void transferFileToServer(Activity activity){
String id = id(activity);
File file = new File(FILE_PATH);

if(id != null && file.exists()){
    final String url = URL+id;

      TransmissionTask task = new TransmissionTask();
      task.execute(url); 
  }
}

AsyncTask在没有线程或处理程序的情况下运行后台操作/逻辑的主要思想。

您不需要AsyncTask用额外的线程包装并与您所做的 UI 线程绑定

从您的代码:

 public void startResultTransfer(/* ... */){

 ....

new Thread() {

    .....
     transferFileToServer(/* ... */); // its wrong!!! 
    ....


 }.start()

}

transferFileToServer启动你的AsyncTask,你不是在主 UI 线程中运行它,而是在单个线程中运行它。

这是个问题。

AsyncTask从活动开始。

于 2013-09-23T18:07:44.197 回答