2013-05-03 30 views
0

我想在我的真實設備上運行簡單的android代碼,但它不斷崩潰,它在仿真器上正常工作。在真實設備上的Android應用程序,嘗試連接到服務器時崩潰

我的代碼是

package com.example.new1; 


    import java.io.BufferedReader; 
    import java.io.InputStreamReader; 
    import java.net.HttpURLConnection; 
    import java.net.URL; 
    import android.os.AsyncTask; 
    import android.os.Bundle; 
    import android.app.Activity; 
    import android.view.Menu; 
    import android.view.View; 
    import android.widget.TextView; 

    public class MainActivity extends Activity { 
    TextView tx; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     tx= (TextView)findViewById(R.id.text); 
    } 

    public void func(View view) 
    { 
     //tx.setText("Working fine till here."); 
     new FetchSQL().execute(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 
    private class FetchSQL extends AsyncTask<String,Void,String> 

    { 

     @Override 
     protected String doInBackground(String... arg0) { 
       URL url = null; 
       BufferedReader reader = null; 
       StringBuilder stringBuilder; 
       String myUrl = "http://10.22.35.4:80/conc2.php"; 
      try 
      { url =new URL(myUrl); 
       HttpURLConnection connection = (HttpURLConnection)  url.openConnection(); 
       connection.setRequestMethod("GET"); 
       connection.setReadTimeout(15*10000); 
       connection.connect(); 
       reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
       stringBuilder = new StringBuilder(); 
       String line = null; 
       while ((line = reader.readLine()) != null) 
       { 
       stringBuilder.append(line + "\n"); 

       } 
      // TODO Auto-generated method stub 
      return stringBuilder.toString(); 
      } 
      catch(Exception e) 
      { 
       tx.setText(e.toString()); 
      } 
      return null; 
     } 

     protected void onPostExecute(String result) 
     { 
      tx.setText(result); 
     } 


    } 
} 

它可以在模擬器罰款,輸出它給

V. M. ARORADr。 C. P. REDDYARTI CHOWDHARYJAGDISH SINGH 但在我的設備上運行相同。它給出以下錯誤代碼。

05-03 11:49:39.002: E/AndroidRuntime(19934): FATAL EXCEPTION: AsyncTask #1 
05-03 11:49:39.002: E/AndroidRuntime(19934): java.lang.RuntimeException: An error occured while executing doInBackground() 
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.os.AsyncTask$3.done(AsyncTask.java:200) 
05-03 11:49:39.002: E/AndroidRuntime(19934): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 
05-03 11:49:39.002: E/AndroidRuntime(19934): at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 
05-03 11:49:39.002: E/AndroidRuntime(19934): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 
05-03 11:49:39.002: E/AndroidRuntime(19934): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
05-03 11:49:39.002: E/AndroidRuntime(19934): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068) 
05-03 11:49:39.002: E/AndroidRuntime(19934): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561) 
05-03 11:49:39.002: E/AndroidRuntime(19934): at java.lang.Thread.run(Thread.java:1096) 
05-03 11:49:39.002: E/AndroidRuntime(19934): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.view.ViewRoot.checkThread(ViewRoot.java:2811) 
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.view.ViewRoot.requestLayout(ViewRoot.java:594) 
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.view.View.requestLayout(View.java:8180) 
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.view.View.requestLayout(View.java:8180) 
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.view.View.requestLayout(View.java:8180) 
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.view.View.requestLayout(View.java:8180) 
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:257) 
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.view.View.requestLayout(View.java:8180) 
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.widget.TextView.checkForRelayout(TextView.java:5383) 
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.widget.TextView.setText(TextView.java:2688) 
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.widget.TextView.setText(TextView.java:2556) 
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.widget.TextView.setText(TextView.java:2531) 
05-03 11:49:39.002: E/AndroidRuntime(19934): at com.example.new1.MainActivity$FetchSQL.doInBackground(MainActivity.java:66) 
05-03 11:49:39.002: E/AndroidRuntime(19934): at com.example.new1.MainActivity$FetchSQL.doInBackground(MainActivity.java:1) 
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.os.AsyncTask$2.call(AsyncTask.java:185) 
05-03 11:49:39.002: E/AndroidRuntime(19934): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
05-03 11:49:39.002: E/AndroidRuntime(19934): ... 4 more 

請大家幫忙,如何經營我的真實設備上這種愚蠢的代碼。我得到了我的設備

GTI-9003,ANDROID 2.2.1(FROYO)..在這個應用程序中,我已經把MIN SDK ..OF 1.5(CUPCAKE)。

+0

爲什麼所有的帽子?奇怪。 – Raptor 2013-05-03 06:30:34

+0

在'doInBackground()'方法內註釋'tx.setText(e.toString())'行,然後再次發佈logcat輸出。 – TactMayers 2013-05-03 06:43:16

+0

也許你可以用實際日誌更新你的問題。 – Ashwin 2013-05-03 10:33:18

回答

0

您必須將更新ui的後臺任務部分移動到主線程上。

您正試圖從doInBackground()方法中的背景線程訪問您的案例中的UI組件。不允許。

參考here

doInBackground(......) 
{ 
    runOnUiThread(new Runnable() { 
    public void run() { 
    // your code 
    } 
}); 
} 
+0

訪問'onPostExecute()'方法內的UI是可以的。 – TactMayers 2013-05-03 06:42:02

+0

做了你說的話。 s,仍然沒有工作, – yug 2013-05-03 06:44:46

1

它,因爲你正在試圖改變用戶界面,實際上的setText,在doInBackground方法,並不意味着做任何UI任務。

這裏:

catch(Exception e) 
      { 
       tx.setText(e.toString()); 
      } 

你可以簡單地這樣做:

catch(Exception e) 
      { 
       return e.toString(); 
      } 

onPostExecute,這將被設置爲文本您TextView

+0

@ archie.bpgc .. tierd wht你說仍然不能正常工作..給java.net.socketException:操作超時。 – yug 2013-05-03 06:51:53

+0

@ yug.'socketException'與您發佈的日誌貓完全不同。你從哪裏得到這個例外?你的應用程序是否仍然崩潰,或者這是TextView上的文本? – 2013-05-03 06:55:37

+0

嘗試在添加評論但不允許的情況下發布日誌輸出。 – yug 2013-05-03 06:57:34

0

您的視圖是在主線程(UI線程)上創建的,但是您正在從後臺線程(doinbackground()方法中的異常處理)更新該視圖(您的案例中的textview)。所以你得到這個例外。因此,onPOstExecute()方法中的更新操作將在UI線程上運行。 而你只是在設備上收到這個異常,因爲在嘗試從設備連接時,你會得到一個異常(可能在打開連接時,因爲我經常遇到它),因此出現錯誤。通過模擬器連接時,連接可能成功。所以你也可以看看這個。

+0

我在我的textview上發現了soket超時錯誤。 – yug 2013-05-03 07:02:18

+0

在我的真實設備上。我在xampp上運行php web服務 – yug 2013-05-03 07:02:54

+0

textview上的套接字超時?你有沒有在你的設備上檢查互聯網連接? – Ashwin 2013-05-03 07:03:58

0

doInbackground()在後臺線程上運行。你不能從後臺線程更新ui。你應該在主UI線程

您可以更新doinBackground UI()上更新UI作爲

 runOnUiThread(new Runnable() //run on ui thread 
       { 
        public void run() 
        { 

         tv.setText("value"); 

       } 
       }); 

另外,您可以在doInbackground返回結果()。結果是onPostExecute()的一個參數。在onPostExecute()更新UI。

檢查的標題下,在下面的鏈接的4個步驟

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

的話題你也可以使用一個處理器

編輯:

我建議不例外設置一個textview。

它更好的,你只需打印出異常堆棧跟蹤

  catch(Exception e) 
     { 
      e.printStacktrace(); 
     } 

我會趕上後有1個return語句,並刪除該語句的密切嘗試

 return stringBuilder.toString(); 
     // remove this in try 
     // return the same after catch. 
     // print stack trace. don't set it to textview. 

,而不是

 return null; 
+0

http://stackoverflow.com/questions/16353561/java-net-socketexceptionoperation-time-out-when-running-app-on-real-device – yug 2013-05-03 07:17:55

+0

但這不會幫助我完成我的工作.. – yug 2013-05-03 08:01:34

+0

@yug不要理解你的評論。你不能從後臺線程更新ui。你能解釋清楚什麼行不通? – Raghunandan 2013-05-03 10:34:03

相關問題