2012-12-06 89 views
3

在同一個類上運行我的代碼時,應用程序運行良好。但是當我在兩個不同的類上運行此代碼時,我的應用程序出現錯誤android.os.networkonmainthreadexception。 當我調試我發現錯誤在responseHttp = httpConnection.getResponseCode(); 應用程序運行至行responseHttp = httpConnection.getResponseCode();它去抓{},它取消「If..else」。並記錄錯誤android.os.networkonmainthreadexception。 你能幫助我嗎!錯誤android.os.networkonmainthreadexception asynctask getResponseCode

我的代碼類的AsyncTask

package com.example.finishdemo; 

import java.net.HttpURLConnection; 
import java.net.URL; 
import java.net.URLConnection; 
import android.os.AsyncTask; 

public class TestConnectionNew extends AsyncTask<String, Void, String> { 
private int responseHttp = 0; 
private String flag="false"; 

@Override 
protected String doInBackground(String... urltest) { 
    // TODO Auto-generated method stub 
    try { 
     URL url = new URL(urltest[0]); 
     URLConnection connection = url.openConnection(); 
     connection.setConnectTimeout(2000); 
     HttpURLConnection httpConnection = (HttpURLConnection) connection; 
     **responseHttp = httpConnection.getResponseCode();** 
     if (responseHttp == HttpURLConnection.HTTP_OK) { 
      flag = "true"; 
     } else { 
      flag = "false"; 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
     System.out.println("Thong bao loi: "+e.toString()); 
    } 
    return flag; 
} 
} 

代碼類主:

package com.example.finishdemo; 
public class Hoadon extends Activity { 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.hoadon); 
    TestConnectionNew t=new TestConnectionNew(); 
    String recieve=t.doInBackground("http://longvansolution.tk/monthlytarget.php"); 
    if(recieve.equalsIgnoreCase("true")) 
    { 
     doTimerTask(); 
    }else 
     if(recieve.equalsIgnoreCase("false")) 
    { 
     showAlert("Không kết nối được đến server hoặc thiết bị chưa có kết nối internet!"); 
    } 
+0

我不覺得這是怎麼你需要調用一個AsyncTask。 –

回答

7

使用asynctask.execute執行的AsyncTask,而不是手動調用doInBackground爲:

TestConnectionNew t = new TestConnectionNew(); 
t.execute("http://longvansolution.tk/monthlytarget.php"); 

更改TestConnectionNew爲

public class TestConnectionNew extends AsyncTask<String, Void, String> { 
private int responseHttp = 0; 
private String flag="false"; 

@Override 
protected String doInBackground(String... urltest) { 
    // TODO Auto-generated method stub 
    try { 
     URL url = new URL(urltest[0]); 
     URLConnection connection = url.openConnection(); 
     connection.setConnectTimeout(2000); 
     HttpURLConnection httpConnection = (HttpURLConnection) connection; 
     **responseHttp = httpConnection.getResponseCode();** 
     if (responseHttp == HttpURLConnection.HTTP_OK) { 
      flag = "true"; 
     } else { 
      flag = "false"; 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
     System.out.println("Thong bao loi: "+e.toString()); 
    } 
    return flag; 
} 

@Override 
    protected void onPostExecute(String recieve) { 
      if(recieve.equalsIgnoreCase("true")) 
      { 
       doTimerTask(); 
      }else 
      if(recieve.equalsIgnoreCase("false")) 
      { 
       showAlert("Không kết nối được đến server hoặc thiết bị chưa có kết nối internet!"); 
      } 
     } 
} 

和了解我們如何使用的AsyncTask看到更多的幫助:

http://developer.android.com/reference/android/os/AsyncTask.html

+0

當我添加代碼「String recieve = t.execute(」http://longvansolution.tk/monthlytarget.php「);」並收到此通知「類型不匹配:無法從AsyncTask 轉換爲字符串」 – Takeshi

+0

@ user1817079:請參閱我的編輯更新UI的答案,您將需要Override onPostExecute –

+0

,如果您使用String recieve = t.execute ( 「longvansolution.tk/monthlytarget.php」)獲得();那麼這也將掛起UI線程,直到控制不從AsyncTask返回到主線程 –

0

在AndroidManifest.xml 您添加代碼:

uses-permission android:name="android.permission.INTERNET" 
uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" 
相關問題