private static void postParamters () throws Exception
{
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("Package_Tracking_No", "2");
String query = builder.build().getEncodedQuery();
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush(); writer.close(); os.close(); }
Code for posting a request to PHP Script
Sr.No Component & description
1 Array([)In a JSON file , square bracket ([) represents a
JSON array
2 Objects({)In a JSON file, curly bracket ({) represents a
JSON object
3 KeyA JSON object contains a key that is just a string.
Pairs of key/value make up a JSON object
4 ValueEach key has a value that could be string , integer
or double e.t.c
JSONObject json = new JSONObject (result);
Result is returned as Json Object
After Object, Next Turn is Array
Gets an integer data from PHP Script saved in “ptn”
JSONObject Json = new JSONObject (response);
int s= Json.getInt("success");
JSONArray pr = Json.getJSONArray("products");
pr.getJSONObject(i).getInt("ptn")
{
"products":
[
{"ptn":"1","sts":"ok","dd":"12/11/2004"},
{"ptn":"1","sts":"ok","dd":"12/11/2004"}
],
"success":1
}
Success:1 is a json object.
Products is a json array.
Ptn is json object in Json array
JSON: JavaScript Object Notation.
JSON is a syntax for storing and exchanging data.
JSON is an easier to use alternative to XML.
JSON syntax is basically considered as subset of JavaScript syntax,
it includes the following:
1.Data is represented in name/value pairs
2.Curly braces hold objects and each name is followed by
':'(colon), the name/value pairs are separated by , (comma).
3.Square brackets hold arrays and values are separated by
,(comma).
2 Events An XML file has many events. Event could
be like this. Document starts , Document ends, Tag
start , Tag end and Text e.t.c
3 Text Apart from tags and events, and xml file also
contains simple text. Such as GB is a text in the
country tag.
4 Attributes Attributes are the additional properties
of a tag such as value e.t.c
<item>
<id>1</id>
<name>Margherita</name>
<cost>155</cost>
<description id=1 >Single cheese topping</description>
</item>
getName()
This method returns the name of the tag
getText()
This method returns the text for that
particular element
getAttributeCount()
This method just Returns the number of
attributes of the current start tag
getAttributeName(int index)
This method returns the name of the attribute specified
by the index value
public String getRequest (String urlString)
{
InputStream stream=null;
try {
// connection will be made here
stream = conn.getInputStream();
xmlFactoryObject = XmlPullParserFactory.newInstance(); // a parser is initialized to not process namespaces
XmlPullParser xmlparser = xmlFactoryObject.newPullParser();
xmlparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
xmlparser.setInput(stream, null); //and to use the provided InputStream as its input
parseXML(xmlparser); // extracts and processes the data the app is interested in:
stream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return result;
}
public void parseXML (XmlPullParser myParser) {
try {
event = myParser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
String name=myParser.getName();
switch (event){
case XmlPullParser.START_TAG:
break;
case XmlPullParser.TEXT:
text = myParser.getText();
break;
case XmlPullParser.END_TAG:
if(name.equals("name")){
result+= text;
}
else if ( name.equals("id"))
{
result+= text;
}
}
event = myParser.next();}