Beispiel #1
0
 // 通过URL发送post请求,返回请求结果
 public static String queryStringForPost(String url) {
   // 获得HttpPost实例
   HttpPost request = HttpUtil.getHttpPost(url);
   String result = null;
   try {
     // 获得HttpResponse实例
     HttpResponse response = HttpUtil.getHttpResponse(request);
     // 判断是否请求成功
     rc = response.getStatusLine().getStatusCode();
     if (AppConstant.HS_OK == rc) {
       // 获得返回结果
       result = EntityUtils.toString(response.getEntity());
       return result;
     }
   } catch (ClientProtocolException e) {
     e.printStackTrace();
     result = "网络异常!";
     return result;
   } catch (IOException e) {
     e.printStackTrace();
     result = "网络异常!";
     return result;
   }
   return null;
 }
Beispiel #2
0
 // 通过URL发送get请求,返回请求结果
 public static String queryStringForGet(String url) {
   // 获得HttpGet实例
   HttpGet request = HttpUtil.getHttpGet(url);
   System.out.println("HttpGet");
   String result = null;
   try {
     // 获得HttpResponse实例
     HttpResponse response = HttpUtil.getHttpResponse(request);
     System.out.println("HttpResponse");
     // 判断是否请求成功
     if (AppConstant.HS_OK == response.getStatusLine().getStatusCode()) {
       // 获得返回结果
       result = EntityUtils.toString(response.getEntity());
       return result;
     }
   } catch (ClientProtocolException e) {
     e.printStackTrace();
     result = "error";
     return result;
   } catch (IOException e) {
     e.printStackTrace();
     result = "error";
     return result;
   } catch (TimeoutException e) {
     // TODO: handle exception
     e.printStackTrace();
     result = "error";
     return result;
   }
   return null;
 }
Beispiel #3
0
 @SuppressWarnings("finally")
 // 通过HttpGet发送get请求
 public static boolean cmdForGet(String url) {
   boolean result = false;
   // 获得HttpGet实例
   HttpGet request = HttpUtil.getHttpGet(url);
   try {
     // 获得HttpResponse实例
     HttpResponse response = HttpUtil.getHttpResponse(request);
     // 判断是否请求成功
     rc = response.getStatusLine().getStatusCode();
     if (AppConstant.HS_OK == rc) {
       // 获得返回结果
       result = true;
     } else {
       System.out.println("GET return " + rc);
     }
   } catch (ClientProtocolException e) {
     e.printStackTrace();
     System.out.println("网络异常!");
     result = false;
   } catch (IOException e) {
     e.printStackTrace();
     System.out.println("网络异常!");
     result = false;
   } finally {
     return result;
   }
 }
  private void updateChart() {
    String url = gConfig.URL.BASE_URL + "query_temperature";
    System.out.println("url: " + url);
    String xml_base = HttpUtil.queryStringForGet(url);
    System.out.println("HttpGet end");
    System.out.println("get xml: " + xml_base);
    if (xml_base.equals("error")) {
      Toast.makeText(app_query_temperature_Activity.this, "网络异常", Toast.LENGTH_SHORT).show();
    } else {
      try {
        // 创建一个SAXParserFactory
        SAXParserFactory factory = SAXParserFactory.newInstance();
        XMLReader reader = factory.newSAXParser().getXMLReader();
        // 将XMLReader设置内容处理器
        reader.setContentHandler(new ContentHandler_temperature());
        // 开始解析文件
        reader.parse(new InputSource(new StringReader(xml_base)));
      } catch (Exception e) {
        System.out.println("xml parse error");
        e.printStackTrace();
      }
    }

    series.clear();
    Cursor cursor =
        dbReadHandle.query(
            gConfig.testTableName, new String[] {"time", "value"}, null, null, null, null, null);
    java.util.Date date = new Date(0);
    Calendar cal = Calendar.getInstance();
    cal.get(Calendar.HOUR_OF_DAY); // 得到24小时机制的
    // date.get(Calendar.HOUR);// 得到12小时机制的

    while (cursor.moveToNext()) {
      String time_s = cursor.getString(cursor.getColumnIndex("time"));
      int time_i = Integer.parseInt(time_s); // 413,1352
      System.out.println("time_i: " + time_i);

      System.out.println(
          "time: "
              + "2013 "
              + (time_i / 1000000 + 1)
              + "."
              + time_i % 1000000 / 10000
              + " "
              + time_i % 10000 / 100
              + ":"
              + time_i % 100);
      cal.set(Calendar.YEAR, 2013);
      cal.set(Calendar.MONTH, time_i / 1000000);
      cal.set(Calendar.DATE, time_i % 1000000 / 10000);
      cal.set(Calendar.HOUR_OF_DAY, time_i % 10000 / 100);
      cal.set(Calendar.MINUTE, time_i % 100);

      date = cal.getTime();

      String yValue = cursor.getString(cursor.getColumnIndex("value"));
      // System.out.println("time: " + time + " value: " + value);
      // series.add(cursor.getPosition() * 10,
      // Double.parseDouble(yValue));
      series.add(date, Double.parseDouble(yValue));
      System.out.println("value_double: " + Double.parseDouble(yValue));
    }
    // 移除数据集中旧的点集
    mDataset.removeSeries(series);
    // 在数据集中添加新的点集
    mDataset.addSeries(series);
    // 视图更新,没有这一步,曲线不会呈现动态
    // 如果在非UI主线程中,需要调用postInvalidate(),具体参考api
    // chart.invalidate();
    mChartView.invalidate();

    //
    // // 设置好下一个需要增加的节点
    // addX = 0;
    // // addY = (int)(Math.random() * 90);
    // addY = (int) (Math.random() * 50);
    // // 移除数据集中旧的点集
    // mDataset.removeSeries(series);
    // // 判断当前点集中到底有多少点,因为屏幕总共只能容纳100个,所以当点数超过100时,长度永远是100
    // int length = series.getItemCount();
    // if (length > 100) {
    // length = 100;
    // }
    // // 将旧的点集中x和y的数值取出来放入backup中,并且将x的值加1,造成曲线向右平移的效果
    // for (int i = 0; i < length; i++) {
    // xv[i] = (int) series.getX(i) + 1;
    // yv[i] = (int) series.getY(i);
    // }
    // // 点集先清空,为了做成新的点集而准备
    // series.clear();
    // // 将新产生的点首先加入到点集中,然后在循环体中将坐标变换后的一系列点都重新加入到点集中
    // // 这里可以试验一下把顺序颠倒过来是什么效果,即先运行循环体,再添加新产生的点
    // series.add(addX, addY);
    // for (int k = 0; k < length; k++) {
    // series.add(xv[k], yv[k]);
    // }
    // // 在数据集中添加新的点集
    // mDataset.addSeries(series);
    //
    // // 视图更新,没有这一步,曲线不会呈现动态
    // // 如果在非UI主线程中,需要调用postInvalidate(),具体参考api
    // chart.invalidate();
  }