在我的MainActivity頂我所做的:「不幸的是應用程序已經停止,」 由於NetworkOnMainThreadException
private static final int MY_DATA_CHECK_CODE = 0;
public static MainActivity currentActivity;
TextToSpeech mTts;
private String targetURL;
private String urlParameters;
private Button btnClick;
private String clicking = "clicked";
private String[] ipaddresses = new String[]{
private String iptouse = "";
private TextView text;
private boolean connectedtoipsuccess = false;
private int counter = 0;
private NotificationCompat.Builder mbuilder;
private Timer timer = new Timer();
private TextView text1, text2, text3;
private Button startButton;
private Button pauseButton;
private TextView timerValue;
private long startTime = 0L;
private Handler customHandler = new Handler();
long timeSwapBuff = 0L;
private int servercheckCounter = 0;
private byte[] checkServer = null;
private boolean connectedSuccess = false;
隨後的onCreate裏面我做:在updateTimerThread
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread,0);
然後我做:
private Runnable updateTimerThread = new Runnable() {
public void run() {
long updatedTime = 0L;
long timeInMilliseconds = 0L;
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
servercheckCounter +=1 ;
if (servercheckCounter == 10) {
if(connectedSuccess == true) {
checkServer = Get(iptouse + "result");
if (checkServer != null) {
String a = null;
try {
a = new String(checkServer, "UTF-8");
textforthespeacch = a;
MainActivity.currentActivity.initTTS();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
servercheckCounter = 0;
}
int secs = (int) (updatedTime/1000);
int mins = secs/60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
timerValue.setText("" + mins + ":" + String.format("%02d", secs) + ":" + String.format("%03d", milliseconds) + " counter: " + String.format("%01d",servercheckCounter));
customHandler.postDelayed(this, 100);
}
};
定時器運行無停止,我每秒鐘都會發送一個命令給我的Web服務器我的電腦,這是在計時器線程發送命令行:
checkServer = Get(iptouse + "result");
GET方法是:
private byte[] Get(String urlIn) {
URL url = null;
String urlStr = urlIn;
if (urlIn != null)
urlStr=urlIn;
try {
url = new URL(urlStr);
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
}
HttpURLConnection urlConnection = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
byte[] buf=new byte[10*1024];
int szRead = in.read(buf);
byte[] bufOut;
if (szRead==10*1024) {
throw new AndroidRuntimeException("the returned data is bigger than 10*1024.. we don't handle it..");
} else {
bufOut = Arrays.copyOf(buf, szRead);
}
return bufOut;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (urlConnection!=null)
urlConnection.disconnect();
}
}
我想要做的就是發送每秒的命令在我的電腦上的網絡服務器,如果/當網絡服務器發生什麼事情,它會返回一個值給客戶端的Java是客戶端。
的問題是,它在做這行後:
checkServer = Get(iptouse + "result");
一次或兩次它拋出的Android設備屏幕上的錯誤。 我試圖調試它,但我找不到確切的地方,什麼原因以及如何解決問題。
我就行了Get方法增加了一個破發點:
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
這是越來越此行停止在這條線,然後當我提出繼續有時停在這條線再次有時拋出的錯誤有時一次拋出錯誤。
而且我在更多的地方使用我的程序向網絡服務器發送命令,它只在這個地方工作得很好,我試圖每秒發送一個命令它正在造成問題。
我想要做的事就是我認爲的所謂的池。
也許問題是我每秒發送一個命令,而Get方法由於速度太快而無法處理它?
問題在於它沒有停止Get方法中的任何catch位置。
這是從logcat的錯誤消息:
09-03 17:16:46.476 17224-17224/com.test.webservertest E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.adilipman.webservertest, PID: 17224, android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:249)
at libcore.io.IoBridge.recvfrom(IoBridge.java:553)
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:485)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
at com.android.okio.Okio$2.read(Okio.java:113)
at com.android.okio.RealBufferedSource.indexOf(RealBufferedSource.java:147)
at com.android.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:94)
at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:175)
at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:101)
at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:616)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:379)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:323)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:190)
at com.adilipman.webservertest.MainActivity.Get(MainActivity.java:582)
at com.adilipman.webservertest.MainActivity.access$700(MainActivity.java:72)
at com.adilipman.webservertest.MainActivity$3.run(MainActivity.java:188)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5274)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
你的logcat說什麼? –
如果我們只能得到錯誤日誌 –
由於您將UI代碼與網絡代碼混合在一起,我假設它是'NetworkOnMainThreadException'。在主線程的'Handler'上運行的'Runnable'仍然在主UI線程上運行。 http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – laalto