Anúncio

Mais conteúdo relacionado

Apresentações para você(20)

Anúncio
Anúncio

Php sql-android

  1. JSONUseActivity onClick(View v) PrimeTask OnPostExecute() doInBackground(ArrayList<NameValuePair>, url) CustomHttpClient getHttpClient() executeHttpPost(String url,ArrayList<NameValuePair> postParameters) URLConnectionClient PostParameters(String url) conn.getInputStream() AsyncTask<String, Void , String> doInBackground(url) onPostExecute(String result)
  2. JSONUseActivity onClick(View v) PrimeTask OnPostExecute() doInBackground(ArrayList<NameValuePair>, url) CustomHttpClient getHttpClient() executeHttpPost(String url,ArrayList<NameValuePair> postParameters) 11: Start the Task And send URL URLConnectionClient PostParameters(String url) conn.getInputStream() AsyncTask<String, Void , String> doInBackground(url) onPostExecute(String result)
  3. JSONUseActivity onClick(View v) PrimeTask OnPostExecute() doInBackground(ArrayList<NameValuePair>, url) CustomHttpClient getHttpClient() executeHttpPost(String url,ArrayList<NameValuePair> postParameters) 11: Start the Task URLConnectionClient PostParameters(String url) conn.getInputStream() AsyncTask<String, Void , String> doInBackground(url) onPostExecute(String result)
  4. JSONUseActivity onClick(View v) PrimeTask OnPostExecute() doInBackground(ArrayList<NameValuePair>, url) CustomHttpClient getHttpClient() executeHttpPost(String url,ArrayList<NameValuePair> postParameters) URLConnectionClient PostParameters(String url) conn.getInputStream() AsyncTask<String, Void , String> doInBackground(url) onPostExecute(String result) 11: Start the Task
  5. JSONUseActivity onClick(View v) PrimeTask OnPostExecute() doInBackground(ArrayList<NameValuePair>, url) CustomHttpClient getHttpClient() executeHttpPost(String url,ArrayList<NameValuePair> postParameters) 1 URLConnectionClient PostParameters(String url) conn.getInputStream() AsyncTask<String, Void , String> doInBackground(url) onPostExecute(String result) 4: Do in background Parse the Json Response and Send Attributes to the UI via On PostExecute 1: Start the Task
  6. JSONUseActivity onClick(View v) PrimeTask OnPostExecute() doInBackground(ArrayList<NameValuePair>, url) CustomHttpClient getHttpClient() executeHttpPost(String url,ArrayList<NameValuePair> postParameters) 1 4: Converts the JsonArray into String and Pass it OnPostExecute 1: Start the Task 5: Pass the Result to UI Activity via OnPostExecute
  7. public class JSONUseActivity extends Activity { . . . . public void onClick(View v) { DownloadTask t = new DownloadTask(); t.execute(url); } }); }
  8. public class DownloadTask extends AsyncTask<String, Void , String> { protected String doInBackground(String... params) { String url= params[0]; try { response= URLConnectionClient.PostParameters(url); // Parsing will be implemented here } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return str; } protected void onPostExecute(String result) { tv.setText(result);
  9. public static String post(String url) throws Exception { try { URL serveraddress = new URL(url); conn = (HttpURLConnection) serveraddress.openConnection(); conn.setRequestMethod("POST"); conn.setDoInput(true); conn.setDoOutput(true); conn.setChunkedStreamingMode(0); postParamters (); conn.connect(); result= handleResponse(); } catch (IOException e) { e.printStackTrace(); } return result; }
  10. private static String handleResponse () throws IOException { String jsonResponse=""; // "Package_Tracking_No","2" int responseCode=conn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { String line; BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream())); while ((line=br.readLine()) != null) { jsonResponse+=line; } } else { jsonResponse=""; } return jsonResponse;}
  11. 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
  12. 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
  13. 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
  14. 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).
  15. { "book": [ { "id“ : “01", "language“ : "Java", "edition“ : "third", "author“ : "Herbert Schildt" }, { "id“ : "07", "language“ : "C++", "edition“ : "second" "author“ : "E.Balagurusamy" } ] } This shows individual examples of XML and JSON: JSON { "company": Volkswagen, "name": "Vento", "price": 800000 } XML <car> <company>Volkswagen</company> <name>Vento</name> <price>800000</price> </car>
  16. 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>
  17. 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
  18. 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; }
  19. 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();}
Anúncio