Saturday, 21 April 2012

Read the php service in android using json object

Note: the rules should followed as given bellow



Setting must be in manifesto.xml

***************************************************************************
<uses-permission android:name="android.permission.INTERNET"></uses-permission>


if the version android 4.0

StrictMode  need to be added..... before calling the value of service



StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
   


*******************************************************************************

this is the code for reading through the url



package com.mmad.restaurant;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class RestService {
     JSONArray jArray;
     String result = null;
     InputStream is = null;
     StringBuilder sb = null;
       
    public String GetValue(int f,int t)
    {
     try {
               HttpClient httpclient = new DefaultHttpClient();
               // HttpPost httppost = new
               // HttpPost("http://192.168.40.23:9999/ppp.php?id=1");
               // HttpPost httppost = new
               // HttpPost("http://192.168.40.23:7777/domain_master_Mujusam.php");
               HttpPost httppost = new HttpPost("http://192.168.40.24:5050/web-services/Rest_Service/order_details.php?f="+f+"&t="+t);
               HttpResponse response = httpclient.execute(httppost);
               HttpEntity entity = response.getEntity();
               is = entity.getContent();

              } catch (Exception e) {
               Log.e("log_tag", "Error in http connection" + e.toString());
              }
              // convert response to string
              try {
               BufferedReader reader = new BufferedReader(new InputStreamReader(
                 is, "iso-8859-1"), 8);
               sb = new StringBuilder();
               sb.append(reader.readLine() + "\n");

               String line = "0";
               while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
               }
               is.close();
               result = sb.toString();
              } catch (Exception e) {
               Log.e("log_tag", "Error converting result " + e.toString());
              }
              return result;
       
    }
   
   
   
}


this is code for getting the json object



JSONArray jArray = new JSONArray(getTab);
                   for(int j=0;j<jArray.length();j++){
                      
                            JSONObject json_data = jArray.getJSONObject(j);
                           
                            String orderItem=json_data.getString("oi_foddname");
                            String orderQty=json_data.getString("oi_foodqty");
}








No comments:

Post a Comment