Java 从thingspeak网站向HelloChart检索数据
在此代码中,数据取自thingspeak网站,给定频道id。该频道为公共频道。该图是使用hellocharts和chartview获得的 问题: 我想打印图形上绘制的所有数据值或文本视图中添加的最后一个数据。哪个变量是存储的数据,因为我想进一步处理这些数据 在输出中,我想知道值395存储在哪个变量中 我在下面提供了java代码Java 从thingspeak网站向HelloChart检索数据,java,android,Java,Android,在此代码中,数据取自thingspeak网站,给定频道id。该频道为公共频道。该图是使用hellocharts和chartview获得的 问题: 我想打印图形上绘制的所有数据值或文本视图中添加的最后一个数据。哪个变量是存储的数据,因为我想进一步处理这些数据 在输出中,我想知道值395存储在哪个变量中 我在下面提供了java代码 import android.graphics.Color; import android.os.Bundle; import android.support.v7.a
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.Toast;
import com.macroyau.thingspeakandroid.ThingSpeakChannel;
import com.macroyau.thingspeakandroid.ThingSpeakLineChart;
import com.macroyau.thingspeakandroid.model.ChannelFeed;
import java.util.Calendar;
import java.util.Date;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.LineChartView;
public class DemoActivity extends ActionBarActivity {
private ThingSpeakChannel tsChannel;
private ThingSpeakLineChart tsChart;
private LineChartView chartView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Connect to ThinkSpeak Channel 9
tsChannel = new ThingSpeakChannel(135855);
// Set listener for Channel feed update events
tsChannel.setChannelFeedUpdateListener(new ThingSpeakChannel.ChannelFeedUpdateListener() {
@Override
public void onChannelFeedUpdated(long channelId, String channelName, ChannelFeed channelFeed) {
// Show Channel ID and name on the Action Bar
getSupportActionBar().setTitle(channelName);
getSupportActionBar().setSubtitle("Channel " + channelId);
// Notify last update time of the Channel feed through a Toast message
Date lastUpdate = channelFeed.getChannel().getUpdatedAt();
Toast.makeText(DemoActivity.this, lastUpdate.toString(), Toast.LENGTH_LONG).show();
}
});
// Fetch the specific Channel feed
tsChannel.loadChannelFeed();
// Create a Calendar object dated 5 minutes ago
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, -5);
// Configure LineChartView
chartView = (LineChartView) findViewById(R.id.chart);
chartView.setZoomEnabled(false);
chartView.setValueSelectionEnabled(true);
// Create a line chart from Field1 of ThinkSpeak Channel 9
tsChart = new ThingSpeakLineChart(135855, 2);
// Get 200 entries at maximum
tsChart.setNumberOfEntries(200);
// Set value axis labels on 10-unit interval
tsChart.setValueAxisLabelInterval(10);
// Set date axis labels on 5-minute interval
tsChart.setDateAxisLabelInterval(10);
// Show the line as a cubic spline
tsChart.useSpline(true);
// Set the line color
tsChart.setLineColor(Color.parseColor("#D32F2F"));
// Set the axis color
tsChart.setAxisColor(Color.parseColor("#455a64"));
// Set the starting date (5 minutes ago) for the default viewport of the chart
// tsChart.setChartStartDate(calendar.getTime());
// Set listener for chart data update
tsChart.setListener(new ThingSpeakLineChart.ChartDataUpdateListener() {
@Override
public void onChartDataUpdated(long channelId, int fieldId, String title, LineChartData lineChartData, Viewport maxViewport, Viewport initialViewport) {
// Set chart data to the LineChartView
chartView.setLineChartData(lineChartData);
// Set scrolling bounds of the chart
chartView.setMaximumViewport(maxViewport);
// Set the initial chart bounds
chartView.setCurrentViewport(initialViewport);
/* LineChartData data = new LineChartData();
float data1=data.getBaseValue();
TextView tvName = (TextView)findViewById(R.id.textView);
tvName.setText((int) data1);*/
}
});
// Load chart data asynchronously
tsChart.loadChartData();
}
}
根据,我相信您是在异步加载图表数据之后查找的
chartView.getLineChartData()
从这一点开始,您应该能够深入到Line
对象,并且Line.getValues()
将您想要的数据作为
总而言之
List<PointValue> values = chartView.getLineChartData().getLines().getValues();
// TODO: for (PointValue p : values) { p.getX(); p.getY(); }
List values=chartView.getLineChartData().getLines().getValues();
//TODO:for(PointValue p:values){p.getX();p.getY();}
此列表包含一个具有
getX
和getY
方法的类我不知道这个特定的库,但要访问您的数据,您只需点击给定通道的API即可,即
String lightApi = "https://api.thingspeak.com/channels/<your_channel_id>/fields/<field_number>.json?api_key=<read_API_key>&results=<number_of_readings>";
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,lightApi, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray feeds = response.getJSONArray("feeds");
for(int i=0; i<feeds.length();i++){
JSONObject jo = feeds.getJSONObject(i);
String l=jo.getString("<your_field_number>"); //you may want to Integer.parseInt the returned value
//String date=jo.getString("created_at"); -->to get the date and time
// Do whatever you want to do with each of these values...
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
stringlightapi=”https://api.thingspeak.com/channels//fields/.json?api_key=&results=";
JsonObjectRequest JSONObjectReq=新的JsonObjectRequest(Request.Method.GET,lightApi,null,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
JSONArray提要=response.getJSONArray(“提要”);
对于(int i=0;i获取日期和时间
//用这些值做任何你想做的事情。。。
}
}捕获(JSONException e){
e、 printStackTrace();
}
}
}
我看不到你从哪里获得图表的X和Y值…tsChart.loadChartData();
-这是在做什么?查看ThingSpeakLineChart
的源代码,它没有公开这些信息。你能提供声明line对象和line.getvalues()的代码吗是否将返回字符串?我没有使用该库。我只是在阅读Github上的源代码。Android Studio甚至应该自动完成这些方法,这样您就可以看到返回的内容。您可以提供引用源代码的Github链接吗?在.getValues()中出现错误在行列表中,values=chartView.getLineChartData().getLines().getValues();在自动更正时,它会添加API索引,但仍然存在错误。运行时错误或编译时错误?我从未保证它会返回数据,但这是您需要的方法链。您可能需要检查空值等