Manipulating Data on Webs in Android

Manipulating data on webs in Android

You can use jsoup to parse HTML. Here you can find the jsoup library and full source code.

Here is an example: http://desicoding.blogspot.com/2011/03/how-to-parse-html-in-java-jsoup.html

To install in Eclipse:

  1. Right Click on project
  2. BuildPath
  3. Add External Archives
  4. select the .jar file

How can I manipulate a website based on input from an android application?

Basically, what you need to get this working is to use an application programming interface (API). An API lets your program communicate with other programs such as PayPal. Your app will ask for PayPal for some data (perhaps an account number to send the money to) and PayPal will send a response. This response will either be a success message or a failure message depending on how PayPal's API handles your request. The way that these messages are sent and received is via HTTP Requests. If everything goes smoothly, your app will talk back and forth with PayPal's API.

You will need to do more research on this topic to understand all the concepts involved. You may want to start by looking into the HTTP request life cycle, and RESTful API.

Getting data from the web using Android?

I eventually found something that is exactly what I wanted: HtmlCleaner. There's a good guide here.

Download the JAR file here and include it in the project's library.

Then use the following code to get your element from the XPath:

public class Main extends Activity {

// HTML page
static final String URL = "https://www.yourpage.com/";
// XPath query
static final String XPATH = "//some/path/here";

@Override
public void onCreate(Bundle savedInstanceState) {
// init view layout
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// decide output
String value = getData();
}

public String getData() {
String data = "";

// config cleaner properties
HtmlCleaner htmlCleaner = new HtmlCleaner();
CleanerProperties props = htmlCleaner.getProperties();
props.setAllowHtmlInsideAttributes(false);
props.setAllowMultiWordAttributes(true);
props.setRecognizeUnicodeChars(true);
props.setOmitComments(true);

// create URL object
URL url = new URL(URL);
// get HTML page root node
TagNode root = htmlCleaner.clean(url);

// query XPath
Object[] statsNode = root.evaluateXPath(XPATH);
// process data if found any node
if(statsNode.length > 0) {
// I already know there's only one node, so pick index at 0.
TagNode resultNode = (TagNode)statsNode[0];
// get text data from HTML node
stats = resultNode.getText().toString();
}

// return value
return data;
}
}

How send data to website by using android app

Since Android 3.x you can't perform network operations in main thread. You need to use AsyncTask or separate thread where you can call your postData method.

http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

Web Scraping Android Application

Try JSoup for extracting and manipulating HTML data.

Accessing database or webservice

Since you want to access a remote server which has your DB and other stuff.
You basically have two options.

1 - ) Either directly access your server via sockets etc.

2 - ) Or create a web service which will connect your server to the outside. ( I.e : Android Client,iOs client etc.)

The best practice is to create a web service and then consume it in your Android application.

Check these for consuming XML in Android.

http://www.ibm.com/developerworks/opensource/library/x-android/

http://www.warriorpoint.com/blog/2009/07/19/android-reading-using-and-working-with-xml-data-and-web-services-in-android/

Check these for creating web services on server side. (Stack independent)

http://davidwalsh.name/web-service-php-mysql-xml-json (PHP)

http://www.roseindia.net/webservices/buildingsimplewebservice.shtml (Java)

http://www.codeguru.com/csharp/csharp/cs_webservices/article.php/c19391/Creating-a-NET-Web-Service.htm (.NET)

And so on there are a lot of stacks to create web services.

How can I prevent an Android WebView from manipulating a web page?

It is not really possible to prevent the web response being modified. You can only think of making it difficult for the spoofer to edit it.



Related Topics



Leave a reply



Submit