Android login form using Post -
i trying create post request following api:
consumer.api.mobdev.machies.com
in order this, have written following code:
httppost httppost = new httppost("http://consumer.api.mobdev.machies.com/v3/logins"); try { // add user name , password edittext uname = (edittext)findviewbyid(r.id.username); string username = uname.gettext().tostring(); edittext pword = (edittext)findviewbyid(r.id.password); string password = pword.gettext().tostring(); list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(3); namevaluepairs.add(new basicnamevaluepair("grant_type","password")); namevaluepairs.add(new basicnamevaluepair("username", username)); namevaluepairs.add(new basicnamevaluepair("password", password)); httppost.setheader("host","http://consumer.api.mobdev.machies.com"); httppost.setheader("content-type","application/xml"); httppost.setheader("accept-language","en-us"); httppost.setentity(new urlencodedformentity(namevaluepairs)); // execute http post request log.w("post", "execute http post request"); httpresponse response = httpclient.execute(httppost); string str = inputstreamtostring(response.getentity().getcontent()).tostring();
str refers response get. upon printing in logcats, got following response saying invalid hostname. there wrong hostname i've put in code? please new using rest style apis
may use httpclient here may helps you
httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(strurl); if (mheader != null) { (header header : mheader) { httppost.addheader(header); } } try { multipartentity entity = new multipartentity(httpmultipartmode.browser_compatible); if (namevaluepair != null) { contentbody cb; for(namevaluepair value: namevaluepair){ cb = new stringbody(value.getvalue(),"", null); entity.addpart(value.getname(), cb); } } if(filepath != null && filepath.length() > 0){ file f = new file(filepath); entity.addpart("uploadedfile", new filebody(f)); } httppost.setentity(entity); httpresponse httpresponse = httpclient.execute(httppost); response = entityutils.tostring(httpresponse.getentity()); } catch (clientprotocolexception e) { e.printstacktrace(); //display(); } catch (ioexception e) { e.printstacktrace(); //display(); } if (misprogress) progress.dismiss(); return response; }
Comments
Post a Comment