public String getemptyroom(String paramString1, String paramString2,String sign,String xuenian,String xueqi,String jieshu,String xingqiji)
{
String info;
    try
    {
      HttpGet localHttpGet = new HttpGet("http://jwxt2.cumt.edu.cn/(k1nmc4uhbxrnxr2zopscc0ut)/xxjsjy.aspx?xh=" + paramString1 + "&xm=" + paramString2 + "&gnmkdm=N121611");
      localHttpGet.setHeader("Referer", "http://jwxt2.cumt.edu.cn/(k1nmc4uhbxrnxr2zopscc0ut)/xs_main.aspx?xh=" + paramString1);
      HttpClient localHttpClient = this.httpClient;
      HttpResponse localHttpResponse = localHttpClient.execute(localHttpGet);
      HttpEntity localHttpEntity =localHttpResponse.getEntity();
      org.jsoup.nodes.Document localDocument = Jsoup.parse(EntityUtils.toString(localHttpEntity));
      String str3 = localDocument.getElementsByAttributeValue("name", "__VIEWSTATE").first().attr("value");
      if (localDocument.getElementsByTag("title").first().html().equals("欢迎使用正方教务管理系统!请登录"))
      {
        info = "-4";
        System.out.println(info);
      }
      localHttpGet.abort();
      HttpPost localHttpPost = new HttpPost("http://jwxt2.cumt.edu.cn/(k1nmc4uhbxrnxr2zopscc0ut)/xxjsjy.aspx?xh=" + paramString1 + "&xm=" + paramString2 + "&gnmkdm=N121611");;
      localHttpPost.setHeader("Referer", "http://jwxt2.cumt.edu.cn/(k1nmc4uhbxrnxr2zopscc0ut)/xxjsjy.aspx");
      localHttpPost.setHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)");
      localHttpPost.setHeader("Connection", "Keep-Alive");
      localHttpPost.setHeader("Host", "jwxt2.cumt.edu.cn");
      localHttpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
      ArrayList<BasicNameValuePair> localArrayList = new ArrayList<BasicNameValuePair>();
      localArrayList.add(new BasicNameValuePair("__VIEWSTATE", str3));
     localArrayList.add(new BasicNameValuePair("Button2", "空教室查询"));
      localArrayList.add(new BasicNameValuePair("ddlSyXn", xuenian));
      localArrayList.add(new BasicNameValuePair("dpDataGrid1:txtPageSize", "400"));
     localArrayList.add(new BasicNameValuePair("ddlSyxq", xueqi));
      localArrayList.add(new BasicNameValuePair("jslb", "公用多媒体"));
      localArrayList.add(new BasicNameValuePair("jsbh", "教1-A101"));
      localArrayList.add(new BasicNameValuePair("sjd", jieshu));
      localArrayList.add(new BasicNameValuePair("xiaoq", "2"));
     localArrayList.add(new BasicNameValuePair("xn", xuenian));
      localArrayList.add(new BasicNameValuePair("xq", xingqiji));
      localArrayList.add(new BasicNameValuePair("xqj", xingqiji));
      localArrayList.add(new BasicNameValuePair("jssj", sign));
      localArrayList.add(new BasicNameValuePair("kssj", sign));
      UrlEncodedFormEntity localUrlEncodedFormEntity = new UrlEncodedFormEntity(localArrayList, "UTF-8");
      localHttpPost.setEntity(localUrlEncodedFormEntity);
      HttpClient localHttpClient1 = this.httpClient;
      HttpResponse localHttpResponse1 = localHttpClient1.execute(localHttpPost);
      HttpEntity localHttpEntity2 = localHttpResponse1.getEntity();
      String str5 = EntityUtils.toString(localHttpEntity2);
      return str5;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
      return "0";
    }
}这个代码在网络正常和链接不超时的情况下能正常使用,但当服务器繁忙或者服务器维护或者网址错误时程序就会异常退出。求大神能不能插入一些try catch 语句,捕获这些异常。我刚接触网络编程,不知道应该插入什么地方。android,网络编程,超时异常

解决方案 »

  1.   

    语言基础不过关啊。
    你单步调试,看是在那里抛出的异常,抛出的是什么异常,比如执行下面一句抛出NetworkException异常的话:
    connection.openConnection();//你就这样写,try
    {
        connection.openConnection();//
    }
    catch(NetworkException ex)
    {
        ex.getMessage();
    }
    catch(Exception ex)
    {
        ex.getMessage();
    }
      

  2.   

    localHttpClient.execute(localHttpGet);
    我猜是这句吧,你自己看文档,execute这个方法会抛出什么异常,
      

  3.   

    不行啊,Throws
    IOException in case of a problem or the connection was aborted
    ClientProtocolException in case of an http protocol error.另外,那个this.localHttp是这样初始化变量的HttpParams httpParams = new BasicHttpParams();  
             HttpConnectionParams.setConnectionTimeout(httpParams,  
                     6000);  
             HttpConnectionParams.setSoTimeout(httpParams,  
                     6000);
         localHttpClient = new DefaultHttpClient(httpParams);能不能把6000改成10然后捕获超时异常。。我试了几个地方都捕获不到。
      

  4.   

    你是在真机上测试的还是模拟器上测的?建议你在真机上试一下,用gprs或者3g网,不要用wifi,另外,链接超时时间可以设置短一点试下
      

  5.   

    你这么多参数 建议用json  以流的形式传,给个方法你参考
    public static String postHttpResponseText(String url, String post) {
    BufferedReader reader = null;
    HttpURLConnection conn = null;
    try {
    URL httpUrl = new URL(url);
    HttpURLConnection httpConn = (HttpURLConnection) httpUrl
    .openConnection();
    // //设置连接属性
    httpConn.setDoOutput(true);// 使用 URL 连接进行输出
    httpConn.setDoInput(true);// 使用 URL 连接进行输入
    httpConn.setUseCaches(false);// 忽略缓存
    httpConn.setRequestMethod("POST");// 设置URL请求方法
    String requestString = post;
    // 设置请求属性
    // 获得数据字节数据,请求数据流的编码,必须和下面服务器端处理请求流的编码一致
    byte[] requestStringBytes = requestString.getBytes("UTF-8");
    httpConn.setRequestProperty("Content-length", ""
    + requestStringBytes.length);
    httpConn.setRequestProperty("Content-Type", "application/json");
    httpConn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
    httpConn.setRequestProperty("Charset", "UTF-8");
    // 建立输出流,并写入数据
    OutputStream outputStream = httpConn.getOutputStream();
    outputStream.write(requestStringBytes);
    outputStream.close();
    // 获得响应状态
    int responseCode = httpConn.getResponseCode();
    if (HttpURLConnection.HTTP_OK == responseCode) {// 连接成功
    // 当正确响应时处理数据
    StringBuffer buffer = new StringBuffer();
    String readLine = null;
    // 处理响应流,必须与服务器响应流输出的编码一致
    reader = new BufferedReader(new InputStreamReader(
    httpConn.getInputStream(), "UTF-8"));
    while ((readLine = reader.readLine()) != null) {
    buffer.append(readLine).append("\n");
    }
    reader.close();
    return buffer.toString();
    }
    } catch (Exception e) {
    } finally {
    if (reader != null) {
    try {
    reader.close();
    } catch (IOException e1) {
    }
    }
    if (conn != null) {
    conn.disconnect();
    }
    }
    return null; }