Http post Request to Ibm Personality insights with Android -


hi make http post request ibm service personality insight using android. tried use code:

        private string mserver="gateway.watsonplatform.net";         private int mport = 9081;         private string muser= "**userid hidden**";         private string mpassword= "**password hidden**";          private httpresponse makerequest(string urlpath) throws exception {             httpclient httpclient;             httpparams httpparameters;             httppost request;              int timeoutconnection = 10000;             int timeoutsocket = 10000;              httpparameters = new basichttpparams();             httpconnectionparams.setconnectiontimeout(httpparameters, timeoutconnection);             httpconnectionparams.setsotimeout(httpparameters, timeoutsocket);              httpclient = new defaulthttpclient(httpparameters);             credentials creds = new usernamepasswordcredentials(muser, mpassword);             ((abstracthttpclient) httpclient).getcredentialsprovider().setcredentials(                     new authscope(mserver, mport), creds);             request = new httppost("https://gateway.watsonplatform.net/personality-insights/api/v2/profile");              httpresponse res = httpclient.execute(request);             httpclient.getconnectionmanager().shutdown();              return res;         }          private string processrequest(httpresponse resp) throws exception{             int status = resp.getstatusline().getstatuscode();              if (status == 200){                 //get body                 return getbodyfromresponse(resp);             } else if (status == 400) {                 //bad request                 throw new exception("error: http 400 error");             } else if (status == 401) {                 //unauthorized                 throw new exception("error: http 401 error: incorrect server name");             } else if (status == 403) {                 //forbidden                 throw new exception("error: http 403 error: check username , password");             } else if (status == 500) {                 //internal server error                 throw new exception("error: http 500 error");             } else {                 throw new exception("error unknown: status " + status);             }         }          //http response passed in, , json string body returned         private string getbodyfromresponse(httpresponse resp) throws exception{             responsehandler<string> handler = new basicresponsehandler();             try {                 return handler.handleresponse(resp);             } catch (ioexception e) {                 throw new exception ("error: http response 200. error when converting response. " + e.getmessage());             } catch (exception e) {                 throw new exception ("error: http response 200. error when converting response. " + e.getmessage());             }         } 

however got these errors:

    05-14 17:19:36.124  29241-29241/com.example.antonio.helloword w/system.err﹕ android.os.networkonmainthreadexception     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1118)     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ java.net.inetaddress.lookuphostbyname(inetaddress.java:385)     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ java.net.inetaddress.getallbynameimpl(inetaddress.java:236)     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ java.net.inetaddress.getallbyname(inetaddress.java:214)     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ org.apache.http.impl.conn.defaultclientconnectionoperator.openconnection(defaultclientconnectionoperator.java:137)     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ org.apache.http.impl.conn.abstractpoolentry.open(abstractpoolentry.java:164)     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ org.apache.http.impl.conn.abstractpooledconnadapter.open(abstractpooledconnadapter.java:119)     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ org.apache.http.impl.client.defaultrequestdirector.execute(defaultrequestdirector.java:360)     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:555)     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:487)     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:465)     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ com.example.giovanni.helloword.login.makerequest(login.java:115)     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ com.example.giovanni.helloword.login.oncreate(login.java:68)     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ android.app.activity.performcreate(activity.java:5047)     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1094)     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ android.app.activitythread.performlaunchactivity(activitythread.java:2056)     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ android.app.activitythread.handlelaunchactivity(activitythread.java:2117)     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ android.app.activitythread.access$700(activitythread.java:134)     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ android.app.activitythread$h.handlemessage(activitythread.java:1218)     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ android.os.handler.dispatchmessage(handler.java:99)     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ android.os.looper.loop(looper.java:137)     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ android.app.activitythread.main(activitythread.java:4867)     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ java.lang.reflect.method.invokenative(native method)     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ java.lang.reflect.method.invoke(method.java:511)     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1007)     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ com.android.internal.os.zygoteinit.main(zygoteinit.java:774)     05-14 17:19:36.194  29241-29241/com.example.antonio.helloword w/system.err﹕ @ dalvik.system.nativestart.main(native method) 

i try make http connection ibm service response. looks it's impossible do. how can fix it?

your problems quite common android beginner. log, android.os.networkonmainthreadexception stated doing network task on main thread not allowable. have network task in background otherwise can't away errors. can fix problems wrapping current code asynctask.

 private class myasycntask extends asynctask<string, string, string> {      private string process = null;       protected string doinbackground(string... urls) {         // network task in background          httpresponse res = makerequest(urls[0]);          this.process = processrequest(res);           return getbodyfromresponse(res);      }       protected void onpostexecute(string responsebody) {          // ui task in main thread          showdialog("downloaded " + result + " bytes");      }       /*          rest of code below here          ....      */  } 

you should understand relationship between background , main thread, or else can't understand why asynctask needed in networking. more details asynctask, please take looks @ docs.

http://developer.android.com/reference/android/os/asynctask.html


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -