web services - Android webservice call with server authetication -
i need call 1 webservice (https) server authetication android. please me how it.
if try url in browser, asking enter user name , password. how give user name , password while consuming https webservice autheticate?
thanks.
you can use asytask class call webservices,in asyntask class have 4 methods 1.onpreexcute:used show data loading. 2.doinbackground:used sending , loading data , server. 3.onpostexcute:used after data loaded server , here ui realted task have done.
class myasyntask extends asynctask<string, void, string> { string responsestring; @override protected void onpreexecute() { // todo auto-generated method stub super.onpreexecute(); progress = new progressdialog(getactivity()); progress.setmessage("please wait data loading..."); progress.show(); } @override protected string doinbackground(string... params) { string json = null; string url = "your url"; try { // create new httpclient , post header httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); // add data list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>( 5); namevaluepairs.add(new basicnamevaluepair("username", yourusername)); namevaluepairs.add(new basicnamevaluepair("password", yourpassword); httppost.setentity(new urlencodedformentity(namevaluepairs)); // encoding post data try { httppost.setentity(new urlencodedformentity(namevaluepairs)); } catch (unsupportedencodingexception e) { e.printstacktrace(); } // execute http post request httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); responsestring = entityutils.tostring(entity); log.d("mychat reasponse", responsestring); system.out.println(responsestring); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return url; } @override protected void onpostexecute(string string) { // todo auto-generated method stub super.onpostexecute(string); if (progress.isshowing()) { progress.dismiss(); } }
you can response server in json form , according json structor can parse json.
Comments
Post a Comment