我需要用兩個錯誤I'm混得的Android主題/的AsyncTask,和的ExceptionInInitializerError RuntimeException的關於「runOnUiThread」
- 創建線程,在那裏我真的創建文件
- 文件後您的幫助的東西,一個得到的AsyncTask執行將文件發送到服務器(多/表單數據)
那是第一部分的樣子:
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();
}
}
}
}
雖然不是直接的背後你* current *錯誤的問題,你試圖使用runOnUiThread將所有可能的網絡操作放到UI線程上的所有外觀 - 與你應該做的**完全相反** ,它在後臺運行。 –
嗨克里斯,thx。那就是我的想法。好的,我會在沒有異步任務的情況下執行 –
我們看不到您的異步任務類的問題。你可以發佈嗎? Maxim,美國電話: –