java - trying to connect an android app to a server using Android Asynchronous Http Client -


well, simple app button has method onclickbutton. click connect app server. problem here enters onfailure, can't make succesfull connection. have no idea error i'm doing, started learning today , i've spent whole day trying it.

package com.example.pruebasferia.app;  import android.app.progressdialog; import android.content.context; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.textview;  import com.loopj.android.http.asynchttpclient; import com.loopj.android.http.asynchttpresponsehandler;  import org.apache.http.*;  public class mainactivity extends actionbaractivity {      context thisactivitycontext;     progressdialog waitingserverresponse;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          thisactivitycontext = this;     }       @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.main, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         int id = item.getitemid();         if (id == r.id.action_settings) {             return true;         }         return super.onoptionsitemselected(item);     }      public void onclickbutton(view v) {         final textview textview = (textview)findviewbyid(r.id.tv);         // loading         waitingserverresponse = progressdialog.show(thisactivitycontext, "", "conectándose con google.com");          // async http         asynchttpclient client = new asynchttpclient();     //<--- esta es la libreria, tienes que tenerla         //requestparams parameters = new requestparams();         //parameters.put("q", "blah");           client.get("http://www.google.com"/*, parameters*/, new asynchttpresponsehandler()         {             @override             public void onsuccess(string response)             {                 //string parsedresponse = new string(response);                 textview.settext("respuesta : "+ response );                 log.d("blah", "respuesta del server: " + response );             }              @override             public void onfailure(int statuscode, header[] headers, byte[] responsebody, throwable error)             {                 textview.settext("tulapirula fallo");                 log.d("error", "error " + statuscode);             }              @override             public void onfinish()             {                 // cierra loading                 waitingserverresponse.dismiss();             }         });     }  } 

you need add following permissomn in manifest file.

<uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" /> 

or try https instead of http in url.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -