//gettin my splisttfrom server
String getMySPListFromServer(String key) {
String getMySPList=null;
mDBHelper =mAppMgr.getDbHelper();
getMySPList=connect("http://50.28.26.182:8080/SMS/spList?sessionId="+mSessionId+"&client_type=mc&catalog_version_id=0&profile_id="+key);
String status=JSONSMParser.parseStatus(getMySPList);
Log.d("splist",getMySPList);
if(status.equals("200")){
mMySPProfiles=JSONSMParser.parseMySPList(getMySPList);
createMySPProfiles( mDBHelper,key);
Log.e(TAG+"created profiles","profiles");
return status;
}
else
return status;
}
/**
* This method connects to server through HTTP request .
*/
private static String connect(String url){
// Create the httpclient
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
// Execute the request
HttpResponse response;
// return string
String returnString = null;
try {
// Open the webpage.
response = httpclient.execute(httpget);
// if(response.getStatusLine().getStatusCode() == 200){
if(true) {
// Connection was established. Get the content.
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if (entity != null) {
// A Simple JSON Response Read
InputStream instream = entity.getContent();
// Load the requested page converted to a string into a JSONObject.
// JSONObject myAwway = new JSONObject(convertStreamToString(instream));
returnString = convertStreamToString(instream);
// Log.e(TAG,"instream : "+convertStreamToString(instream));
// Log.e(TAG,"myawway : "+ myAwway);
// Get the query value'
//String query = myAwway.getString("query");
// JSONObject menuObject =myAwway.getJSONObject("photos");
// Make array of the suggestions
// JSONArray suggestions =menuObject.getJSONArray("photo");
// Build the return string.
// returnString = "Found: " + suggestions.length() ;
// for (int i = 0; i < suggestions.length(); i++) {
// returnString += "\n\t" +(suggestions.getJSONObject(i).getString("id").toString());
// }
// returnString = "Found";
// Cose the stream.
instream.close();
}
}
else {
// code here for a response othet than 200. A response 200 means the webpage was ok
// Other codes include 404 - not found, 301 - redirect etc...
// Display the response line.
returnString = "Unable to load page - " + response.getStatusLine();
}
return returnString ;
}
catch (IOException ex) {
// thrown by line 80 - getContent();
// Connection was not established
returnString = "Connection failed; " + ex.getMessage();
}
// catch (JSONException ex){
// JSON errors
// returnString = "JSON failed; " + ex.getMessage();
// }
return returnString;
}
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Log.e(TAG,"sb string : "+ sb.toString());
return sb.toString();
}
/**
* method to parse login status coming from server
*
*/
public static String parseStatus(String jsonString) {
String login_statuscode=null;
String status_msg=null;
try {
JSONObject jObject = new JSONObject(jsonString);
login_statuscode=jObject.getString("status_code");
status_msg=jObject.getString("status_msg");
Log.d("login status", ""+jObject.getString("status_code"));
}
catch (JSONException e) {
e.printStackTrace();
}
return login_statuscode;
}
public static List<ServiceProvider> parseMySPList(String jsonString) {
mMyProlist = new ArrayList<ServiceProvider>();
try {
int noOfUserComments;
int noOfEmailids;
int noOfPhnos;
int noOfServices;
JSONObject jObject = new JSONObject(jsonString);
JSONArray menuitemArray1 = jObject.getJSONArray("MyServiceProvider");
/*
{"MyServiceProvider":[["2002_chakri_edu","Chakrapani","Chakrapani provides technical guidance for CSE Students
","India","hyderabad","4","0","Chakrapani","ICICI","12345555555",[["54843958"]],[["chakri@gmail.com"]],
[[null]],[["7001","100.0","Teaches Computer Engineering Subjects","Computer Engineering by Chakri"]]],[
"2010_sumir_medical","Sumir Bharathi","Sumir provides services on psychology","india","hyderabad","4","0",
"Sumir Bharathi","ICICI","4543759459",[["4358345"]],[["sumir@gmail.com"]]
,[[null]],[["7009","67.89","This course provides introduction to psychology","psychology_introductory_lectur"]]]]}
*/
Log.d("",""+menuitemArray1.length());
UserComments userComments;
Phnos pHno;
ArrayList<UserComments> usercommentsList=new ArrayList<UserComments>();
ArrayList<Phnos> PhnosList=new ArrayList<Phnos>();
Service1 service;
ArrayList<Service1> servicesList=new ArrayList<Service1>();
EmailIds emailid;
ArrayList<EmailIds> emailidList=new ArrayList<EmailIds>();
for(int i=0 ;i<menuitemArray1.length();i++){
ServiceProvider pfl=new ServiceProvider();
JSONArray tempArray = menuitemArray1.getJSONArray(i);
Log.e("menuitemArray1 size",""+menuitemArray1.length());
Log.e("tempArray.length()",""+tempArray.length());
int j=0;
int k=0;
Log.d("Myspdetails", tempArray.getString(k));
Log.d("Myspdetails", tempArray.getString(++k));
Log.d("Myspdetails", tempArray.getString(++k));
Log.d("Myspdetails", tempArray.getString(++k));
Log.d("Myspdetails", tempArray.getString(++k));
Log.d("Myspdetails", tempArray.getString(++k));
Log.d("Myspdetails", tempArray.getString(++k));
Log.d("Myspdetails", tempArray.getString(++k));
Log.d("Myspdetails", tempArray.getString(++k));
Log.d("Myspdetails", tempArray.getString(++k));
//Log.d("user comments size", tempArray.getString(++k));
k=0;
pfl.profile_id=tempArray.getString(k);
pfl.profile_name=tempArray.getString(++k);
pfl.profile_des=tempArray.getString(++k);
pfl.country=tempArray.getString(++k);
pfl.city=tempArray.getString(++k);
pfl.rating=tempArray.getString(++k);
pfl.is_online=tempArray.getString(++k);
pfl.acname=tempArray.getString(++k);
pfl.bankname=tempArray.getString(++k);
pfl.acno=tempArray.getString(++k);
// noOfUserComments = Integer.parseInt(tempArray.getString(++k));
JSONArray tempArray3 =tempArray.getJSONArray(++k);
for( j=0; j<tempArray3.length(); j++){
JSONArray tempArray31 =tempArray3.getJSONArray(j);
pHno=new Phnos(null);
int l=0;
pHno.phno=tempArray31.getString(l);
pHno.profileid=pfl.profile_id;
Log.e("pHno.profileid",""+pHno.profileid);
Log.e("pHno.phno",""+pHno.phno);
PhnosList.add(pHno);
}
JSONArray tempArray4=tempArray.getJSONArray(++k);
for( j=0; j<tempArray4.length(); j++){
JSONArray tempArray41 =tempArray4.getJSONArray(j);
emailid=new EmailIds();
int l=0;
emailid.emailid = tempArray41.getString(l);
emailid.profileid=pfl.profile_id;
Log.e("emailid.profilrid",""+emailid.profileid);
Log.e("emailid.emailid",""+emailid.emailid);
emailidList.add(emailid);
}
JSONArray tempArray1 =tempArray.getJSONArray(++k);
for( j=0; j<tempArray1.length(); j++){
JSONArray tempArray11 =tempArray1.getJSONArray(j);
userComments=new UserComments();
int l=0;
userComments.userComments = tempArray11.getString(l);
userComments.profileid=pfl.profile_id;
Log.e("userComments.profileid",""+userComments.profileid);
Log.e("userComments.userComments",""+userComments.userComments);
usercommentsList.add( userComments);
}
JSONArray tempArray2 =tempArray.getJSONArray(++k);
for( j=0; j<tempArray2.length(); j++){
JSONArray tempArray21 =tempArray2.getJSONArray(j);
int l=0;
service=new Service1();
service.sp_profile_id=pfl.profile_id;
service.service_id = tempArray21.getString(l);
Log.e("service.sp_profile_id",""+service.sp_profile_id);
Log.e("service.service_id",""+service.service_id);
service.service_charge = tempArray21.getString(++l);
Log.e("service_charge",""+service.service_charge);
service.service_name= tempArray21.getString(++l);
service.service_des= tempArray21.getString(++l);
servicesList.add(service);
}
pfl.usercommentsList=usercommentsList;
pfl.phnoList=PhnosList;
pfl.serviceList=servicesList;
pfl.email_idList=emailidList;
mMyProlist.add(pfl);
}
} catch (JSONException e) {
e.printStackTrace();
}
return mMyProlist;
}
public static void createMySPProfiles(DatabaseHelper mDBHelper, String key) {
SQLiteDatabase database = mDBHelper.getWritableDatabase();
ContentValues valueph= new ContentValues();
ContentValues valuemailid= new ContentValues();
ContentValues values3 = new ContentValues();
ContentValues nuprofiles= new ContentValues();
ContentValues valuecomments = new ContentValues();
database.execSQL("delete from nu_subscriptions");
int j=0;
int i =0;
database.execSQL("delete from SM_profile1 ");
Log.e(TAG+"all records deleted ","from SM_profile1");
ContentValues valueser = new ContentValues();
for(;i<mMySPProfiles.size();i++){
String profile_id = mMySPProfiles.get(i).profile_id;
// String category_id = mProfiles.get(i).category_id;
String profile_name=mMySPProfiles.get(i).profile_name;
//String profile_des = mProfiles.get(i).profile_des;
String country=mMySPProfiles.get(i).country;
String city = mMySPProfiles.get(i).city;
String acno=mMySPProfiles.get(i).acno;
String acname=mMySPProfiles.get(i).acname;
String bankname=mMySPProfiles.get(i).bankname;
String rating=mMySPProfiles.get(i).rating;
// String service_id=mProfiles.get(i).service_id;
String is_online=mMySPProfiles.get(i).is_online;
Log.d("profile_id",""+profile_id);
values3.put("profile_id",profile_id);
values3.put("is_online",is_online);
Log.d("profile_name",""+profile_name);
values3.put("profile_name",profile_name);
values3.put("acno",acno);
values3.put("acname",acname);
values3.put("bankname",bankname);
values3.put("country",country);
values3.put("city",city);
values3.put("rating",rating);
//values3.put("videoid",videoid);
database.insert("SM_profile1", null, values3);
}
i=0;
ArrayList<UserComments> usercomments = mMySPProfiles.get(i).usercommentsList;
ArrayList<Service1>serviceList=mMySPProfiles.get(i).serviceList;
ArrayList<EmailIds> emailList=mMySPProfiles.get(i).email_idList;
Log.e("emailList",""+emailList.size());
ArrayList<Phnos> phNoList=mMySPProfiles.get(i).phnoList;
Log.e(" phNoList",""+ phNoList.size());
database.execSQL("delete from SM_email_id1");
database.execSQL("delete from SM_phone_numbers");
for(j=0;j<usercomments.size();j++)
{
String userComments=usercomments.get(j).userComments;
String sp_profile_id = usercomments.get(j).profileid;
String profileid=sp_profile_id;
valuecomments .put("profile_id",sp_profile_id);
valuecomments .put("user_comments",userComments );
database.insert("SM_user_comments1", null,valuecomments);
}
for(j=0;j<emailList.size();j++)
{
String emailid=emailList.get(j).emailid;
String sp_profile_id =emailList.get(j).profileid;
//String profileid=profile_id;
valuemailid.put("profile_id", sp_profile_id);
valuemailid.put("email_id",emailid);
database.insert("SM_email_id1", null,valuemailid);
}
for(j=0;j<phNoList.size();j++)
{
//String sp_profile_id = mMySPProfiles.get(i).profile_id;
String phNo=phNoList.get(j).phno;
String profileid=phNoList.get(j).profileid;
valueph.put("profile_id",profileid);
valueph.put("contact_number",phNo);
database.insert("SM_phone_numbers", null,valueph);
}
for(j=0;j<serviceList.size();j++)
{
String sp_profile_id = serviceList.get(j).sp_profile_id;
String service_id=serviceList.get(j).service_id;
String service_name=serviceList.get(j).service_name;
// String service_des=serviceList.get(j).service_des;
String service_charge=serviceList.get(j).service_charge;
Log.e("sp_profile_id",""+sp_profile_id);
valueser.put("sp_profile_id",sp_profile_id);
valueser.put("service_id",service_id);
valueser.put("service_name",service_name);
Log.e(TAG,""+service_name);
// valueser.put("service_description",service_des);
valueser.put("service_charge",service_charge);
Log.e(TAG,""+service_charge);
valueser.put("nu_profile_id",key);
database.insert("nu_subscriptions", null,valueser);
}
//Log.e(TAG+"mMySPProfiles.size()",""+mMySPProfiles.size());
}
No comments:
Post a Comment