2 u013270296 u013270296 于 2014.05.18 16:43 提问

这个程序为什么接收不了数据,无法显示?Achartengine与socket udp

package com.example.mycharttest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class RtChartsActivity extends Activity {
private int b ;
private DatagramSocket socket;
private DatagramPacket packet;

private Timer timer = new Timer();
private GraphicalView chart;
private TextView textview;
private TimerTask task;
private int addY = -1;
private long addX;
/** 曲线数量 */
private static final int SERIES_NR = 1;
private static final String TAG = "message";
private TimeSeries series1;
private XYMultipleSeriesDataset dataset1;
private Handler handler;
private Random random = new Random();

/** 时间数据 */
Date[] xcache = new Date[20];
/** 数据 */
int[] ycache = new int[20];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rtchart);
    LinearLayout layout = (LinearLayout) findViewById(R.id.linearlayout);
    // 生成图表
    chart = ChartFactory.getTimeChartView(this, getDateDemoDataset(),
            getDemoRenderer(), "hh:mm:ss");
    layout.addView(chart, new LayoutParams(LayoutParams.WRAP_CONTENT, 380));
    // 为TextView添加事件
    textview = (TextView) findViewById(R.id.myview);
    textview.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(RtChartsActivity.this, "ceshiview", 1).show();
            Intent intent = new Intent();
            intent.setClass(RtChartsActivity.this, MainActivity.class);
            startActivity(intent);
        }
    });

    handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // 刷新图表
            updateChart();
            super.handleMessage(msg);
        }
    };
    task = new TimerTask() {
        @Override
        public void run() {
            Message message = new Message();
            message.what = 200;
            handler.sendMessage(message);
        }
    };
    timer.schedule(task, 2 * 1000, 1000);
}

private void updateChart() {
    // 设定长度为20
    int length = series1.getItemCount();
    if (length >= 20)
        length = 20;
    receive();
    addY = random.nextInt()%10;
    addX = b;

    // 将前面的点放入缓存
    for (int i = 0; i < length; i++) {
        xcache[i] = new Date((long) series1.getX(i));
        ycache[i] = (int) series1.getY(i);
    }

    series1.clear();
    // 将新产生的点首先加入到点集中,然后在循环体中将坐标变换后的一系列点都重新加入到点集中
    series1.add(new Date(addX), addY);
    for (int k = 0; k < length; k++) {
        series1.add(xcache[k], ycache[k]);
    }
    // 在数据集中添加新的点集
    dataset1.removeSeries(series1);
    dataset1.addSeries(series1);
    // 曲线更新
    chart.invalidate();
}

public void receive() {

    try {
        socket = new DatagramSocket(4444);
        byte data[] = new byte[1024];
        packet = new DatagramPacket(data, data.length);
        socket.receive(packet);
        String result = new String(packet.getData(),packet.getOffset(),packet.getLength());
        b=Integer.parseInt(result);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

/**
 * 设定如表样式
 * 
 * @return
 */
private XYMultipleSeriesRenderer getDemoRenderer() {
    XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
    renderer.setChartTitle("实时曲线");// 标题
    renderer.setChartTitleTextSize(20);
    renderer.setXTitle("时间"); // x轴说明
    renderer.setAxisTitleTextSize(16);
    renderer.setAxesColor(Color.BLACK);
    renderer.setLabelsTextSize(15); // 数轴刻度字体大小
    renderer.setLabelsColor(Color.BLACK);
    renderer.setLegendTextSize(15); // 曲线说明
    renderer.setXLabelsColor(Color.BLACK);
    renderer.setYLabelsColor(0, Color.BLACK);
    renderer.setShowLegend(false);
    renderer.setMargins(new int[] { 20, 30, 100, 0 });
    XYSeriesRenderer r = new XYSeriesRenderer();
    r.setColor(Color.BLUE);
    r.setChartValuesTextSize(15);
    r.setChartValuesSpacing(3);
    r.setPointStyle(PointStyle.CIRCLE);
    r.setFillBelowLine(true);
    r.setFillBelowLineColor(Color.WHITE);
    r.setFillPoints(true);
    renderer.addSeriesRenderer(r);
    renderer.setMarginsColor(Color.WHITE);
    renderer.setPanEnabled(false, false);
    renderer.setShowGrid(true);
    renderer.setYAxisMax(50);
    renderer.setYAxisMin(-30);
    renderer.setInScroll(true); // 调整大小
    return renderer;
}

/**
 * 数据对象
 * 
 * @return
 */
private XYMultipleSeriesDataset getDateDemoDataset() {
    dataset1 = new XYMultipleSeriesDataset();
    final int nr = 1;
    long value = new Date().getTime();
    Random r = new Random();
    for (int i = 0; i < SERIES_NR; i++) {
        series1 = new TimeSeries("Demo series " + (i + 1));
        for (int k = 0; k < nr; k++) {
            series1.add(new Date(value + k * 1000), 0);
        }
        dataset1.addSeries(series1);
    }
    Log.i(TAG, dataset1.toString());
    return dataset1;
}

@Override
public void onDestroy() {
    // 当结束程序时关掉Timer
    timer.cancel();
    super.onDestroy();
};

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!