2011-03-28 61 views
0
public class tryget extends Activity 
{ 
    TextView result1; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Log.i("GET RESPONSE","result outside"); 
     try 
     { 
      HttpClient client = new DefaultHttpClient(); 
      String getURL = "http://10.0.2.2:8888/mainserver1/androidservlet"; 
      HttpGet get = new HttpGet(getURL); 
      HttpResponse responseGet = client.execute(get); 
      HttpEntity resEntityGet = responseGet.getEntity(); 
      if (resEntityGet != null) 
      { 
       InputStream is = resEntityGet.getContent(); 
       String s=is.toString(); 
       byte[] bytes =s.getBytes(); 

       ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes)); 

       String result = (String) in.readObject(); 


       result1=(TextView)findViewById(R.id.result1); 
       result1.setText(s); 
       result1.findViewById(R.id.result1); 


       //do something with the response 
       //Log.i("GET RESPONSE",EntityUtils.getContentCharSet(resEntityGet)); 
       Log.i("GET RESPONSE",result); 
       // in.close(); 
       is.close(); 
      } 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 

我可以看到應用程序在模擬器中運行,但我不能看到一個包含該值的文本字段「結果」沒有出現在它的空間和logcat的顯示IO.exception當我運行代碼與服務器活着。IO.Exception在logcat的,但我可以看到應用程序運行

+0

什麼是堆棧跟蹤?什麼是字符串結果的輸出?更多信息將不勝感激。 – kkudi 2011-03-28 17:30:50

+0

發佈logcat。這條線可能是錯誤的。 result1.findViewById(R.id.result1); – 2011-03-28 17:37:18

回答

0

您看不到「結果」的值,因爲在達到Log.i("GET RESPONSE",result);之前拋出異常。修復拋出的異常中描述的問題,您將看到您的結果。

此外,而不是在寫catche.printStackTrace();,寫的是這樣的:

Log.e("MY TAG", e.toString(), e);

相關問題