How to connect android app to App engine -


i'm trying connect app in android server i've opened on appengine.

my server's name : dddd-daniel-2345 , website server : http://dddd-daniel-2345.appspot.com/

this how connect android app server in app engine:

app.yaml:

application: dddd-daniel-2345 version: 1 runtime: python27 api_version: 1 threadsafe: yes  handlers:  - url: .*   script: main.app  libraries: - name: webapp2   version: "2.5.2" 

main.py:

#!/usr/bin/env python # # copyright 2007 google inc.  google.appengine.api import users import webapp2   class userhandler(webapp2.requesthandler):     def get(self):         user = users.get_current_user()         if user:             greeting = ('%s,%s,%s' % (user.email(),user.user_id(),user.nickname()))         else:             greeting = ('not logged in')         self.response.out.write(greeting)  app = webapp2.wsgiapplication([('/', userhandler),], debug=true) 

my applications code : add manifast

main.activity:

package com.ap2.demo.comunication;  import android.accounts.account; import android.accounts.accountmanager; import android.content.intent; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.arrayadapter; import android.widget.button; import android.widget.spinner;  import com.ap2.demo.r; import com.ap2.demo.splashactivity;  import org.apache.http.impl.client.defaulthttpclient;  import java.util.arraylist;  public class mainactivity extends actionbaractivity {     accountmanager accountmanager;     private account[] accounts;     spinner spinner;     defaulthttpclient httpclient = new defaulthttpclient();     account account;       @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          accountmanager = accountmanager.get(getapplicationcontext());          // assembling gmail accounts         accounts = accountmanager.getaccountsbytype("com.google");          // add gmail accounts :         arraylist<string> accountlist = new arraylist<string>();         (account account : accounts) {             accountlist.add(account.name);         }          // setting spinner viewed         spinner = (spinner) findviewbyid(r.id.account);         arrayadapter<string> dataadapter = new arrayadapter<string>(this,                 android.r.layout.simple_spinner_item, accountlist);         dataadapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item);         spinner.setadapter(dataadapter);          button startauth = (button) findviewbyid(r.id.startauth);         startauth.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 spinner = (spinner) findviewbyid(r.id.account);                 account = accounts[spinner.getselecteditemposition()];                  accountmanager.getauthtoken(account, "ah", null, false,                         new ontokenacquired(httpclient, mainactivity.this), null);                  intent intent = new intent(mainactivity.this, splashactivity.class);                 startactivity(intent);             }         });      }     protected void onactivityresult(int requestcode, int resultcode, intent data)     {         if (resultcode == result_ok) {             accountmanager.getauthtoken(account, "ah", null, false,                     new ontokenacquired(httpclient, mainactivity.this), null);         }         else if(resultcode ==  result_canceled){             // user canceled         }     }  } 

ontokenacquired:

package com.ap2.demo.comunication;  import android.accounts.accountmanager; import android.accounts.accountmanagercallback; import android.accounts.accountmanagerfuture; import android.app.activity; import android.content.intent; import android.os.bundle;  import org.apache.http.impl.client.defaulthttpclient;  /**  * created daniel on 19-jun-15.  */ /*the result auth token request returned application via account manager callback specified when making request. check returned bundle if intent stored against accountmanager.key_intent key. if there intent start activity using intent ask user permission otherwise can retrieve auth token bundle.*/ public class ontokenacquired implements accountmanagercallback<bundle> {     private static final int user_permission = 989;     private static final string app_id = "dddd-daniel-2345";    // private static final string app_id = "dddd-daniel-1234";     private defaulthttpclient httpclient;     activity activity;      public ontokenacquired(defaulthttpclient httpclient, activity activity)     {         this.httpclient = httpclient;         this.activity   = activity;     }     @override     public void run(accountmanagerfuture<bundle> result) {         bundle bundle;          try {             bundle = (bundle) result.getresult();             if (bundle.containskey(accountmanager.key_intent)) {                 intent intent = bundle.getparcelable(accountmanager.key_intent);                 intent.setflags(intent.getflags() & ~intent.flag_activity_new_task);                 activity.startactivityforresult(intent, user_permission);              } else {                 setauthtoken(bundle);             }         }         catch(exception e){             e.printstacktrace();         }     }      //using auth token , ask auth cookie     protected void setauthtoken(bundle bundle) {         string authtoken = bundle.getstring(accountmanager.key_authtoken);          new getcookie(httpclient, app_id, activity.getbasecontext()).execute(authtoken);     } } 

getcookie:

package com.ap2.demo.comunication;  import android.content.context; import android.os.asynctask;  import org.apache.http.httpresponse; import org.apache.http.client.methods.httpget; import org.apache.http.client.params.clientpnames; import org.apache.http.cookie.cookie; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.params.httpparams;  import java.io.bytearrayoutputstream;  /**  * created daniel on 19-jun-15.  */ public class getcookie extends asynctask<string, void, boolean> {     string appid;     httpparams params;     private httpresponse response;    // private static final string link_to_get_authenticated = "http://dddd-daniel-2345.appspot.com/";     private static final string link_to_get_authenticated = "http://dddd-daniel-2345.appspot.com/";     //private static final string link_to_get_authenticated = "http://dddd-daniel-1234.appspot.com/";     context context;     private defaulthttpclient httpclient;      public getcookie(defaulthttpclient httpclient, string appid, context context)     {         this.httpclient = httpclient;         params = httpclient.getparams();         this.appid = appid;         this.context = context;     }      protected boolean doinbackground(string... tokens) {          try {              // don't follow redirects             params.setbooleanparameter(clientpnames.handle_redirects, false);              httpget httpget = new httpget("http://" + appid                     + ".appspot.com/_ah/login?continue=http://" + appid + ".appspot.com/&auth=" + tokens[0]);             response = httpclient.execute(httpget);             bytearrayoutputstream out = new bytearrayoutputstream();             response.getentity().writeto(out);             out.close();              if(response.getstatusline().getstatuscode() != 302){                 // response should redirect                 return false;             }              //check if received acsid or sacsid cookie, depends on http or https request             for(cookie cookie : httpclient.getcookiestore().getcookies()) {                 if(cookie.getname().equals("acsid") || cookie.getname().equals("sacsid")){                     return true;                 }             }          }  catch (exception e) {             e.printstacktrace();             cancel(true);         } {             params.setbooleanparameter(clientpnames.handle_redirects, true);         }         return false;     }      protected void onpostexecute(boolean result)     {         new auth(httpclient, context).execute(link_to_get_authenticated);     } } 

auth:

package com.ap2.demo.comunication;  import android.content.context; import android.os.asynctask; import android.widget.toast;  import org.apache.http.httpresponse; import org.apache.http.httpstatus; import org.apache.http.statusline; import org.apache.http.client.methods.httpget; import org.apache.http.impl.client.defaulthttpclient;  import java.io.bytearrayoutputstream;  /**  * created daniel on 19-jun-15.  */ public class auth extends asynctask<string, void, boolean> {      private defaulthttpclient httpclient;     private httpresponse response;     private string content =  null;     context context;      public auth(defaulthttpclient httpclient, context context)     {         this.httpclient = httpclient;         this.context = context;     }      protected boolean doinbackground(string... urls) {          try {              httpget httpget = new httpget(urls[0]);             response = httpclient.execute(httpget);             statusline statusline = response.getstatusline();              if(statusline.getstatuscode() == httpstatus.sc_ok){                 bytearrayoutputstream out = new bytearrayoutputstream();                 response.getentity().writeto(out);                 out.close();                 content = out.tostring();                 return true;             }         } catch (exception e) {             e.printstacktrace();             cancel(true);         }         return false;     }      //display response request above     protected void onpostexecute(boolean result) {         toast.maketext(context, "response request: " + content,                 toast.length_long).show();     } } 

activity_main.xml:

<relativelayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:layout_margin="@dimen/activity_vertical_margin">      <textview         android:id="@+id/textview1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignparentleft="true"         android:layout_alignparenttop="true"         android:layout_marginbottom="10dp"         android:text="@string/choose_account"         android:textappearance="?android:attr/textappearancemedium"         android:textstyle="bold" />      <spinner android:id="@+id/account"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_alignparentleft="true"         android:layout_below="@+id/textview1" />      <button         android:id="@+id/startauth"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignparentleft="true"         android:layout_below="@+id/account"         android:layout_margintop="10dp"         android:text="@string/send_button" />  </relativelayout> 

whenever i'm running app shows me correct toast.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -