Tuesday, 10 December 2013

Android Services



package com.mywebservices;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

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.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import org.json.JSONException;
import org.json.JSONObject;

import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class JSONFunctions {




public static JSONObject getJSONfromURL(String url){
InputStream is = null;
String result = "";
JSONObject jArray = null;

//http post
   try{
           HttpClient httpclient = new DefaultHttpClient();
           HttpPost httppost = new HttpPost(url);
           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);
           StringBuilder sb = new StringBuilder();
           String line = null;
           while ((line = reader.readLine()) != null) {
                   sb.append(line + "\n");
           }
           is.close();
           result=sb.toString();
   }catch(Exception e){
           Log.w("log_tag", "Error converting result "+e.toString());
   }
 
   try{
   
            jArray = new JSONObject(result);          
   }catch(JSONException e){
           Log.w("log_tag", "Error parsing data "+e.toString());
   }
 
5% OFF All Lenovo ThinkPad's    return jArray;
}



 public static Bitmap downloadFile(String fileUrl){
 Bitmap bmImg = null;
         URL myFileUrl =null;        
         try {
              myFileUrl= new URL(fileUrl);
         } catch (MalformedURLException e) {
           
              e.printStackTrace();
         }
         try {
              HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
              conn.setDoInput(true);
              conn.connect();
              //int length = conn.getContentLength();
              InputStream is = conn.getInputStream();
           
              bmImg = BitmapFactory.decodeStream(is);
           
         } catch (IOException e) {
              e.printStackTrace();
         }
         return bmImg;
    }


public static JSONObject getJSONfromURL(String url,JSONObject inputjson){
InputStream is = null;
String result = "";
JSONObject jArray = null;

//http post
   try{
           HttpClient httpclient = new DefaultHttpClient();
           HttpPost httppost = new HttpPost(url);
           String inputjsonstring=inputjson.toString();
           StringEntity inEntity = new StringEntity(inputjsonstring);
           inEntity.setContentType("text/plain;charset=UTF-8");
           //entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"text/plain;charset=UTF-8"));
           httppost.setEntity(inEntity);
         
         
           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);
           StringBuilder sb = new StringBuilder();
           String line = null;
           while ((line = reader.readLine()) != null) {
                   sb.append(line + "\n");
           }
           is.close();
           result=sb.toString();
   }catch(Exception e){
           Log.w("log_tag", "Error converting result "+e.toString());
   }
 
   try{
   
           jArray = new JSONObject(result);          
   }catch(JSONException e){
           Log.w("log_tag", "Error parsing data "+e.toString());
   }
 
   return jArray;
}


// http://localhost:8888/allsetbuy/login?cmd=register
void sdf(SQLiteDatabase db){



}

}

Download
click to begin
1.2MB .zip

No comments:

Post a Comment