I want the data from spreadsheet to my application so that i can use the data in my application. I want the data from the spread sheet having two columns i can use the data from the spread sheet.
Just like you do in java. Use Apache-POI or JExcelApi. JExcelAPI is easier to use, but POI is easier on memory. JExcel holds everything in memory. POI is better maintained.
You can use the code of James Moore: very-simple-google-spreadsheet-code
package com.banshee;
import java.io.IOException;
import java.net.URL;
import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.CustomElementCollection;
import com.google.gdata.data.spreadsheet.ListEntry;
import com.google.gdata.data.spreadsheet.ListFeed;
import com.google.gdata.util.ServiceException;
public class SpreadsheetSucker {
public static void main(String[] args) {
SpreadsheetService service = new SpreadsheetService("com.banshee");
try {
// Notice that the url ends
// with default/public/values.
// That wasn't obvious (at least to me)
// from the documentation.
String urlString = "https://spreadsheets.google.com/feeds/list/0AsaDhyyXNaFSdDJ2VUxtVGVWN1Yza1loU1RPVVU3OFE/default/public/values";
// turn the string into a URL
URL url = new URL(urlString);
// You could substitute a cell feed here in place of
// the list feed
ListFeed feed = service.getFeed(url, ListFeed.class);
for (ListEntry entry : feed.getEntries()) {
CustomElementCollection elements = entry.getCustomElements();
String name = elements.getValue("name");
System.out.println(name);
String number = elements.getValue("Number");
System.out.println(number);
}
} catch (IOException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
}
}
}
Related
I'm a newbie in Android Development. I want to get metadata from Shoutcast Server, and found streamscraper to be the easiest one to use. But my problem is, I don't know how to use it. The homepage itself only showing something like in how to use it:
import java.net.URI;
import java.util.List;
import net.moraleboost.streamscraper.Stream;
import net.moraleboost.streamscraper.Scraper;
import net.moraleboost.streamscraper.scraper.IceCastScraper;
public class Harvester {
public static void main(String[] args) throws Exception {
Scraper scraper = new IceCastScraper();
List streams = scraper.scrape(new URI("http://host:port/"));
for (Stream stream: streams) {
System.out.println("Song Title: " + stream.getCurrentSong());
System.out.println("URI: " + stream.getUri());
}
}
}
Searched anywhere and found no project sample of how to use this. I hope one of you can post the code of how to use it, or make a tutorial for it.
No need to use external libraries. The following pages give you:
Current song: http://yourstream:port/currentsong?sid=#
Last 20 songs: http://yourstream:port/played.html?sid#
Next songs: http://yourstream:port/nextsongs?sid=#
An Android java class which prints the current song:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
public class NowPlaying {
public void CurrentSong() {
try
{
URL url = new URL("http://www.mofosounds.com:8000/currentsong?sid=#");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
Note: the nextsongs?sid=#feature must be supported by the player of the stream.
I have a question about parsing (Android Studio). It is not about something in particular. My code just doesn't run. No errors. I want to be able to press a button and show a specific text, parsed from an XML file.
For now, i'll omit the button part and throw you the codes just for printing this text on a humble textview
XML CODE (app/src/main/assets follder. articles.xml)
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE articles[
<!ELEMENT articles ANY>
<!ELEMENT article ANY>
<!ATTLIST article ID ID #IMPLIED> ]>
<articles>
<article ID="a1">TEST
</article>
<article ID="a2">1.TEST2
</article>
CLASS CODE
package com.blah.blah;
import android.content.res.AssetManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
public class MainActivity extends AppCompatActivity {
TextView textView;
String stringArticle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AssetManager assetManager = this.getAssets();
InputStream inputStream = null;
try {
// Loads your XML file as an InputStream
inputStream = assetManager.open("articles");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(inputStream);
Element article = doc.getElementById("a1");
String stringArticle = article.getTextContent();
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(stringArticle);
} catch (IOException e) {
// Exception Handling Code
e.printStackTrace();
}catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
}
I haven't tried the techniques that return you a Nodelist, because I dont want to iterate over anything. I find it useless for my project. What I want is something extremely primitive. It's like those simple bible apps. You press the button with the verse and the corresponding text pops up. That simple.
And again, no errors at all! Just not working. I think the TRY part is not executed. Because it gives me the default 'hello world' text on the TextView. But, once i put the
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(stringarticle);
lines after the TRY/CATCH blocks, I just get an empty TextView.
TY!
You can place your XML file in "app/src/main/assets" directory. After that you can easily access your XML file via "AssetManager" class.
You can use below code as a reference:-
AssetManager assetManager = this.getAssets();
InputStream inputStream = null;
try {
// Loads your XML file as an InputStream
inputStream = assetManager.open("articles.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(inputStream);
Element article = doc.getElementById("1");
String stringarticle = article.getTextContent();
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(stringarticle);
} catch (IOException e) {
// Exception Handling Code
e.printStackTrace();
}catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
Just Remember to place your file in mentioned directory, otherwise "AssetManager" class won't be able to read it.
Cheers !!
I have an odd problem; if I run this code I get a java.io.FileNotFoundException: https://graph.facebook.com/debug_token?input_token=1234&access_token=1234. This only occurs when I call client.getInputStream() inside my AsyncTask. Click the link: it clearly works.
Let's call this case 1.
Now, when I run the exact same code outside of my AsyncTask, I get a NetworkOnMainThreadException, but client.getInputStream() works...
Consider this case 2.
I know why I get the NetworkOnMainThreadException in case 2, but I don't understand why the FileNotFoundException only happens in case 1, and not in case 2. The code is identical! I've been looking at this for hours and I just don't know what I am doing wrong.
EDIT: apperently the FileNotFoundException occurs because of an error response code. I figured this out by getting the error stream with .getErrorStream() when the exception occurs.
import android.os.AsyncTask;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
import javax.net.ssl.HttpsURLConnection;
public class Temp {
private String getResponse(InputStream stream){
Scanner s = new Scanner(stream).useDelimiter("\\A");
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
return s.hasNext() ? s.next() : "";
}
public void run(String url){
URL uri;
HttpsURLConnection client = null;
try {
uri = new URL(url);
client = (HttpsURLConnection) uri.openConnection();
client.setReadTimeout(15*1000);
} catch (IOException e) {
e.printStackTrace();
}
new RetrieveStream().execute(client);
}
private class RetrieveStream extends AsyncTask<HttpsURLConnection, Void, String> {
private String returnString = null; //don't change this!
HttpsURLConnection client;
#Override
protected String doInBackground(HttpsURLConnection... client) {
try {
this.client = client[0];
InputStream stream = this.client.getInputStream();
Log.d(getClass().getSimpleName(), "response: "+getResponse(this.client.getInputStream()));
} catch (FileNotFoundException e) {
Log.d(getClass().getSimpleName(), "error output: "+getResponse(this.client.getErrorStream()));
e.printStackTrace();
} catch (IOException e) {
Log.d(getClass().getSimpleName(), "error: "+getResponse(this.client.getErrorStream()));
e.printStackTrace();
}
this.client.disconnect();
return returnString;
}
protected void onPostExecute(String output) {
Log.d(getClass().getSimpleName(), "output: "+output);
}
}
}
i am not deep into android development, but since the Thread seem to be aware of connections (implied by "NetworkOnMainThreadException"), i'd suggest to handover the URL instance to your AsyncTask and open the connection there, instead of hand over the client.
Apart from this, by reading the api, i'd expect a
client.connect();
before
client.getInputStream();
get's called.
References:
https://developer.android.com/reference/java/net/URL.html#openConnection()
https://developer.android.com/reference/java/net/URLConnection.html#getInputStream()
It's not very clear why the FileNotFoundException occurs, but I was able to get the response with .getErrorStream() instead of .getInputStream(). My question is edited accordingly. Please ignore the other answers, they provide no solutions.
I'm using the library Jamod and I have trouble reading the record, what I want is to read only the record number 300 PLC I'm connected, but I get read error (enters the catch). Thanks for your help
package com.JR.scada;
import java.net.InetAddress;
import net.wimpi.modbus.Modbus;
import net.wimpi.modbus.io.ModbusTCPTransaction;
import net.wimpi.modbus.msg.ReadInputDiscretesRequest;
import net.wimpi.modbus.msg.ReadInputDiscretesResponse;
import net.wimpi.modbus.msg.ReadMultipleRegistersResponse;
import net.wimpi.modbus.net.TCPMasterConnection;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Main extends Activity{
TextView text, depurar;
EditText IP;
Button boton;
int i=0;
TCPMasterConnection con = null; //the TCP connection
ModbusTCPTransaction trans = null; //the Modbus transaction
InetAddress addr = null; //direccion del esclavo
int port = Modbus.DEFAULT_PORT;//puerto por defecto 502
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.lblRegistro);
IP = (EditText) findViewById(R.id.txtIp);
depurar = (TextView) findViewById(R.id.txtdepurar);
boton = (Button)findViewById(R.id.btnVerRegistro);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
protected void onStop() {
super.onStop();
//Close the TCP connection
con.close();
}
public class conectar extends AsyncTask<String,String,Integer>{
ReadInputDiscretesRequest req = null; //the request
ReadInputDiscretesResponse res = null; //the response
int startReg;
protected void onPreExecute() {
try {
//IP address;
addr = InetAddress.getByName("212.170.50.238");
} catch (Exception e) {
Log.d("MODBUS","IP error", e);
}
}
protected Integer doInBackground(String... urls) {
try {
// Open the connection
con = new TCPMasterConnection(addr);
con.setPort(port);
con.connect ();
try {
startReg = 300;
// Prepare the request
req = new ReadInputDiscretesRequest (startReg, 1);
// Prepare the transaction
trans = new ModbusTCPTransaction(con);
trans.setRequest(req);
// execute the transaction
trans.execute();
// get the response
res = (ReadInputDiscretesResponse) trans.getResponse ();
} catch (Exception e) {
Log.d("MODBUS", "Error in reading/writing");
return 1;
}
} catch (Exception e) {
Log.d("MODBUS","connection error", e);
return 1;
}
return 0;
}
protected void onPostExecute(Integer bytes) {
if(con.isConnected()){
depurar.setText("conecta");
}
text.setText("Digital Inputs Status=" + res.getDiscretes ().toString () );
}
}
public void onClick(View v) {
// int startReg;
conectar conectamos = new conectar();
conectamos.execute("hola");
}
error:
08-21 10:01:57.554: D/MODBUS(3322): Error in reading/writing
Without knowing more about the modbus configuration on your slave PLC, my first suggestion is to try a different value for startReg, and see if the error persists. This will rule out problems with your Java.
Some test numbers that should work: 0,1,7,8
They may not all work, but at least one of them should return successfully.
If none of them return successfully, there may be a problem on the PLC configuration, or a problem with your request code.
If one of the test numbers is successful, you should post your results. If so, this means that you are attempting to access a memory location that does not exist on the PLC. If you access undefined PLC memory locations, often the PLC will abort the request (it does not just send back '0').
Followup remarks:
If you find that your code works when using different values for startReg, but not for 300, the reason has to do with memory mapping on the PLC, and this can be different for each brand/model of PLC (post your PLC brand/model if available).
You say in your question 'record 300'. When working with PLC, you usually don't refer to memory as a record. Are you trying to access Bit 300, Byte 300, Word 300, DoubleWord 300?
The real question you need to be asking, is 300 the actual modbus address you want to read, or is 300 how the PLC address is mapped (such as the 300th I/O slot, not necessarily the 300th WORD). It may be required to convert between octal, decimal, and hexidecimal addresses. Or, you may need to re-index an address (some PLCs like to start counting at 1, but generally modbus starts counting at 0).
Perhaps you meant to read BIT 300 (not record 300), which would then be 12th BIT in the 18th WORD, so it would look like this:
//
startReg = 17;
// Prepare the request
// Your 300th bit should be the last value returned.
req = new ReadInputDiscretesRequest (startReg, 12);
The different values I suggested for startReg are meant to help you discover how your PLC Inputs are mapped into modbus addresses by your Java library. This might help with number conversions.
If you keep getting exceptions in your catch block, you might want to find out more about the error.
Try changing this line from your original code:
try {
//...
} catch (Exception e) {
Log.d("MODBUS", "Error in reading/writing");
return 1;
}
into this:
try {
//...
} catch (Exception e) {
Log.d("MODBUS", e.getMessage() );
return 1;
}
Hopefully the exception will tell you more about exactly why its failing. Post those results.
If you are getting a NULL message, you might try using a debugger to manually inspect your connection instance.
How would you get a text file off the internet to download and update pre-determined string names so that the files could be used on a TextView and so the application wouldn't have to be updated when information had to be changed. I have already setup a AsyncTask so the file can be downloaded in the background but how would I get the file downloaded, read and then put into strings and then to have a TextView reloaded so the text could be updated. Any help or code on this would be greatly appreciated and I already have the
protected void doInBackground part setup and ready. I have been having trouble with this for some time so any help could be very handy. I have tried using httppost to get the file but I did not understand what I had to change so it worked. Thanks for reading!
The text file in question is http://nowactivity.webs.com/teststring.txt
Here's a more complete answer:
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import android.os.AsyncTask;
import android.util.Log;
...
#Override
protected String doInBackground(String... params) {
// perform Long time consuming operation
Document doc = null;
String returnValue ="";
String baseWebPage = "http://nowactivity.webs.com/teststring.txt";
for(int i = 0; i< params.length; i++){
try {
doc = Jsoup.connect(
baseWebPage)
.get();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("DOC", "The line " + doc.toString());
}
return returnValue;
}
I'm sure you can mange the rest of your implementation ;-)
More about jsoup can be found here.
Cheers