[問題]用AndroidAPP呼叫WebService(已解決)!!!

看板AndroidDev作者 (ALLENS)時間11年前 (2014/06/04 08:26), 11年前編輯推噓4(405)
留言9則, 5人參與, 最新討論串1/1
各位大大好~ 小弟想試著用AndroidAPP寫一個呼叫自己另外用.Net架設的WebService 但是似乎都沒有反應~ 不知道是否哪邊出問題了? 下面的Code e.getMessage()都是空值~ 所以我才又加一行~把button.text="xxx" 最後運行結果~Button的text都會變成xxx~表示真的有進入exception~ Code如下: public void doSomething(View v) { Button btn = null; btn=(Button)findViewById(R.id.button1); String NAMESPACE = "http://tempuri.org/"; String URL = "http://127.0.0.1:2282/service1.asmx"; String SOAP_ACTION = "http://tempuri.org/HelloWorld"; String METHOD_NAME="HelloWorld"; SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet=true; envelope.setOutputSoapObject(request); HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); try { androidHttpTransport.call(SOAP_ACTION, envelope); SoapPrimitive result = (SoapPrimitive)envelope.getResponse(); btn.setText(result.toString()); } catch(Exception e) { e.printStackTrace(); btn.setText(e.getMessage()); btn.setText("xxx"); } } --------------------------------------------------------------------- 找了一整天~終於找到問題了!!! Android 3.0以上~ 要在OnCreate加上下面的資訊 @SuppressLint("NewApi") //這一行要加 @TargetApi(Build.VERSION_CODES.GINGERBREAD) //這一行要加 @Override protected void onCreate(Bundle savedInstanceState) { //這一整段都要加 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads() .detectDiskWrites() .detectNetwork() // or .detectAll() for all detectable problems .penaltyLog() .build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() .detectLeakedSqlLiteObjects() .penaltyLog() .penaltyDeath() .build()); //-------------------------------------- } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 210.64.29.203 ※ 文章網址: http://www.ptt.cc/bbs/AndroidDev/M.1401841603.A.1A5.html

06/04 08:35, , 1F
ip問題
06/04 08:35, 1F
※ 編輯: aallens (210.64.29.203), 06/04/2014 09:08:35

06/04 09:08, , 2F
ip改成了192.168.x.x的~也不行~
06/04 09:08, 2F

06/04 09:30, , 3F
你用模擬器跑嗎?
06/04 09:30, 3F

06/04 12:27, , 4F
看看網段是否一樣,我常沒同網段就在測(殘唸
06/04 12:27, 4F

06/04 14:35, , 5F
我用手機跑的~~~
06/04 14:35, 5F

06/04 14:47, , 6F
Internet permission?
06/04 14:47, 6F

06/04 15:05, , 7F
哪個也加入了~
06/04 15:05, 7F

06/04 15:17, , 8F
主要在Try的第一行就跳到catch了~
06/04 15:17, 8F

06/04 16:41, , 9F
Exception類別是Android.os.NetworkOnMainThreadException
06/04 16:41, 9F
※ 編輯: aallens (210.64.29.203), 06/04/2014 17:31:37
文章代碼(AID): #1JZcV36b (AndroidDev)