【问题标题】:IO.Exception in the Logcat but I can see the app runningLogcat 中的 IO.Exception 但我可以看到应用程序正在运行
【发布时间】:2011-03-28 17:26:54
【问题描述】:
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。

【问题讨论】:

  • 什么是堆栈跟踪? String 结果的输出是什么?更多信息将不胜感激。
  • 发布日志猫。这条线可能是错误的。 result1.findViewById(R.id.result1);

标签: android http client-server


【解决方案1】:

您看不到“结果”的值,因为在到达Log.i("GET RESPONSE",result); 之前就引发了异常。修复抛出的异常中描述的问题,你会看到你的结果。

另外,不要在catch 中写e.printStackTrace();,而是这样写:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    相关资源
    最近更新 更多