Android 例外:当用户向服务器发送评论时,没有到主机的路由

Android 例外:当用户向服务器发送评论时,没有到主机的路由,android,android-asynctask,Android,Android Asynctask,这种情况很少发生,我也无法复制它,但下面的代码有时会从任何主机获得异常。代码如下: public class AddComment extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... theParams) { String myUrl = theParams[0]; final String

这种情况很少发生,我也无法复制它,但下面的代码有时会从任何主机获得异常。代码如下:

public class AddComment extends AsyncTask<String, Void, String> 
{
    @Override
    protected String doInBackground(String... theParams) 
    {
        String myUrl = theParams[0];
        final String comment = theParams[1];
        final String user_id = theParams[2];
        final String problem_id = theParams[3];
        final String recent_topic_id = theParams[4];

        String charset = "UTF-8";           
        String response = null;

        try 
        {                           
            String query = String.format("comment=%s&user_id=%s&problem_id=%s&recent_topic_id=%s", 
                     URLEncoder.encode( comment, charset), 
                     URLEncoder.encode( user_id, charset), 
                     URLEncoder.encode( problem_id, charset), 
                     URLEncoder.encode( recent_topic_id, charset)
                     );             

            final URL url = new URL( myUrl + "?" + query );

            final HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            conn.setDoOutput(true); 
            conn.setRequestMethod("POST");

            conn.setDoOutput(true);
            conn.setUseCaches(false);

            conn.connect();

            InputStream stream = conn.getInputStream();
            byte[] stream_buffer = new byte[8196];
            int readCount;
            StringBuilder stream_builder = new StringBuilder();
            while ((readCount = stream.read(stream_buffer)) > -1) 
            {
                stream_builder.append(new String(stream_buffer, 0, readCount));
            }

            response = stream_builder.toString();       
        } 
        catch (Exception e) 
        {
                           // EXCEPTION HAPPENS HERE
                e.printStackTrace();
        }

        return response;
    }

    @Override
    protected void onPostExecute(String result) 
    {
           if ( result == null )
           {
                  // Do stuff
           }
        }
        else
        {               
      // Do stuff
    }        
}        
public类AddComment扩展异步任务
{
@凌驾
后台受保护字符串(字符串…参数)
{
字符串myUrl=参数[0];
最终字符串注释=参数[1];
最终字符串user_id=参数[2];
最终字符串问题_id=参数[3];
最后一个字符串最近的主题id=参数[4];
字符串charset=“UTF-8”;
字符串响应=null;
尝试
{                           
String query=String.format(“注释=%s&用户id=%s&问题id=%s&最近的主题id=%s”,
URLEncoder.encode(注释,字符集),
URLEncoder.encode(用户id,字符集),
URLEncoder.encode(问题号,字符集),
编码(最近的主题id,字符集)
);             
最终URL=新URL(myUrl+“?”+查询);
最终的HttpURLConnection conn=(HttpURLConnection)url.openConnection();
连接设置输出(真);
conn.setRequestMethod(“POST”);
连接设置输出(真);
conn.SETUSECHACHES(假);
连接();
InputStream=conn.getInputStream();
字节[]流_缓冲区=新字节[8196];
整数读取计数;
StringBuilder stream_builder=新StringBuilder();
而((readCount=stream.read(stream_buffer))>-1)
{
追加(新字符串(stream_buffer,0,readCount));
}
response=stream_builder.toString();
} 
捕获(例外e)
{
//这里有例外
e、 printStackTrace();
}
返回响应;
}
@凌驾
受保护的void onPostExecute(字符串结果)
{
如果(结果==null)
{
//做事
}
}
其他的
{               
//做事
}        
}        

有人知道为什么会发生这种情况以及如何预防吗?

您是否在物理设备上测试这种情况?这可能是由于手机信号不好,或者与您的设备或模拟器的互联网连接不牢固造成的。这也解释了为什么你不能复制它。

请不要在主题中添加标签