Wednesday, 2 April 2014

Converting getting JSON objects from string pulled from URL using authentication



import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Base64;
import android.util.Log;

public class WebService {

    String domain="http://192.168.1.254/trunk/api/v1/";
  
    JSONObject jsonobj;
    JSONArray jArray;
    String result = null;
    InputStream is = null;
    StringBuilder sb = null;
  
    public List getChildrenMaster(int nurseryID) {
        String urlOnput = null;
        List list=new ArrayList();
        int status;
      
        try {
          
            urlOnput=domain+"installation.php?nursery_id="+nurseryID;
            try {
                JSONObject jsonobj = new JSONObject(getValueResult("http://192.168.1.254/trunk/api/v1/installation.php?nursery_id=1"));
                Log.d("json", jsonobj.toString());
            } catch (Exception e) {
                Log.d("problem in web service", e.toString());
            }          
        //    status = jsonobj.getJSONObject("children").getInt("nursery_id");
            status = jsonobj.getJSONObject("Status").getInt("status");
            System.out.println("**"+status);          
          
        } catch (Exception e) {
            Log.d("Service Error", "Problem in service");
            Log.d("Service Error", ""+e);          
        }
        System.out.println("Service:"+result);
        return list;
    }
  
    public String getValueResult(String url) {  
        System.out.println(url);
      
        String username = "admin";
        String password = "admin";
      
        String unp = username+":"+password;

      
        try {          
            HttpClient httpclient = new DefaultHttpClient();
            //HttpPost httppost = new HttpPost(domain.trim()+"/qhms/wse_hmlogin.php?loginid="+user.trim()+"&logpass="+pass.trim());
          // HttpPost httppost = new HttpPost(domain+"/qhms/wse_hmlogin.php?loginid="+user+"&logpass="+pass);
          //  HttpPost httppost = new HttpPost("http://svv.in.net/service/index.php?user=Customer&pass=cus&des=rtr%20r%20rtr%20trtrt");
            HttpPost httppost = new HttpPost(url);
           // httppost.setHeader( "Authorization","Basic "+"admin:admin");
            String encoded_login = Base64.encodeToString(unp.getBytes(), Base64.NO_WRAP);
            httppost.setHeader(new BasicHeader("Authorization", "Basic "+encoded_login));
         
          
            System.out.println("URL="+httppost.toString());
            // HttpPost httppost = new HttpPost("http://quantumr.info/qrms/call_service/wse_getitem.php?compkey=astres1312");
            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;
    }
}

No comments:

Post a Comment