Skip to content

Instantly share code, notes, and snippets.

@codyherring
Created January 10, 2013 01:48
Show Gist options
  • Save codyherring/4498697 to your computer and use it in GitHub Desktop.
Save codyherring/4498697 to your computer and use it in GitHub Desktop.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.update_activity);
final Handler handler;
handler = new Handler();
final EditText getCustID = (EditText) findViewById(R.id.customer);
final EditText custvar1 = (EditText) findViewById(R.id.var1);
final EditText custvar2 = (EditText) findViewById(R.id.var2);
final EditText custvar3 = (EditText) findViewById(R.id.var3);
class sendGET implements Runnable
{
private String url;
public sendGET(String mUrl)
{
url = mUrl;
}
public void run()
{
try
{
//Declare HttpClient and HttpGet
HttpClient client = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
//Run HttpGet
client.execute(httpget);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class getCustomerData implements Runnable
{
public void run()
{
try
{
InputStream stream = null;
XMLparser xmlParser = new XMLparser();
List<Customer> customers = null;
String parseUrl = URL + getCustID.getText().toString();
try
{
stream = downloadUrl(parseUrl);
customers = xmlParser.parse(stream);
}
finally
{
stream.close();
}
final Customer data = customers.get(0);
handler.post(new Runnable() {
public void run() {
custvar1.setText(String.valueOf(data.var1));
custvar2.setText(String.valueOf(data.var2));
custvar3.setText(String.valueOf(data.var3));
}
});
}
catch (IOException e)
{
//return getResources().getString(R.string.connection_error);
}
catch (XmlPullParserException e)
{
//return getResources().getString(R.string.xml_error);
}
}
private Customer readCustomer(XmlPullParser parser) throws XmlPullParserException, IOException
{
parser.require(XmlPullParser.START_TAG, null, "customer");
String custID = null;
String var1 = null;
String var2 = null;
String var3 = null;
while(parser.next() != XmlPullParser.END_TAG)
{
if (parser.getEventType() != XmlPullParser.START_TAG)
{
continue;
}
String name = parser.getName();
if (name.equals("CustID"))
{
custID = readCustID(parser);
} else if (name.equals("custvar1"))
{
var1 = readVar1(parser);
} else if (name.equals("custvar2"))
{
var2 = readVar2(parser);
} else if (name.equals("custvar3"))
{
var3 = readVar3(parser);
} else
{
skip(parser);
}
}
return new Customer(custID, var1, var2, var3);
}
private String readCustID(XmlPullParser parser) throws IOException, XmlPullParserException
{
parser.require(XmlPullParser.START_TAG, null, "CustID");
final String custID = readText(parser);
parser.require(XmlPullParser.END_TAG, null, "CustID");
return custID;
}
private String readVar1(XmlPullParser parser) throws IOException, XmlPullParserException
{
parser.require(XmlPullParser.START_TAG, null, "custvar1");
String var1 = readText(parser);
parser.require(XmlPullParser.END_TAG, null, "custvar1");
return var1;
}
private String readVar2(XmlPullParser parser) throws IOException, XmlPullParserException
{
parser.require(XmlPullParser.START_TAG, null, "custvar2");
String var2 = readText(parser);
parser.require(XmlPullParser.END_TAG, null, "custvar2");
return var2;
}
private String readVar3(XmlPullParser parser) throws IOException, XmlPullParserException
{
parser.require(XmlPullParser.START_TAG, null, "custvar3");
String var3 = readText(parser);
parser.require(XmlPullParser.END_TAG, null, "custvar3");
return var3;
}
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException
{
String result = "";
if (parser.next() == XmlPullParser.TEXT)
{
result = parser.getText();
parser.nextTag();
}
return result;
}
private void skip(XmlPullParser parser) throws IOException, XmlPullParserException
{
if (parser.getEventType() != XmlPullParser.START_TAG)
{
throw new IllegalStateException();
}
int depth = 1;
while (depth != 0)
{
switch (parser.next()) {
case XmlPullParser.END_TAG:
depth --;
case XmlPullParser.START_TAG:
depth++;
break;
}
}
}
class XMLparser
{
public List<Customer> parse(InputStream in) throws XmlPullParserException, IOException {
try
{
XmlPullParser parser = Xml.newPullParser();
parser.setInput(in, null);
parser.nextTag();
return readXML(parser);
} finally {
in.close();
}
}
private List<Customer> readXML(XmlPullParser parser) throws XmlPullParserException, IOException
{
List<Customer> custData = new ArrayList<Customer>();
parser.require(XmlPullParser.START_TAG, null, "xml");
while (parser.next() != XmlPullParser.END_TAG)
{
if (parser.getEventType() != XmlPullParser.START_TAG)
{
continue;
}
String name = parser.getName();
//Look for customer tag
if (name.equals("customer"))
{
custData.add(readCustomer(parser));
}
else
{
skip(parser);
}
}
return custData;
}
}
private InputStream downloadUrl(String urlString) throws IOException
{
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
//start the query
conn.connect();
InputStream stream = conn.getInputStream();
return stream;
}
}
final Button getData = (Button) findViewById(R.id.getData);
getData.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
//Declare and start thread
final Runnable mGetData = new getCustomerData();
final Thread getData = new Thread(mGetData);
getData.start();
}
});
final Button submit = (Button) findViewById(R.id.Submit);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//set url
String url = "**removed**";
url += "var1=" + custvar1.getText().toString() + "&var2=" + custvar2.getText().toString() + "&var3=" + custvar3.getText().toString();
//Declare and start thread
final Runnable mConnect = new sendGET(url);
final Thread Connect = new Thread(mConnect);
Connect.start();
}
});
}
public class Customer
{
public final String custID;
public final String var1;
public final String var2;
public final String var3;
private Customer()
{
custID = null;
var1 = null;
var2 = null;
var3 = null;
}
private Customer(String custID, String var1, String var2, String var3)
{
this.custID = custID;
this.var1 = var1;
this.var2 = var2;
this.var3 = var3;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment