http - Android login unauthorized -
i work on android project connect web service. there function show data after user login. login function call web service using http post method. here login code connect server:
public httpresponse makerequestnew(string path, string params, string params2) throws clientprotocolexception, ioexception { httpclient = new defaulthttpclient(); httppost httppost = new httppost(path); httppost.setheader("content-type", "application/x-www-form-urlencoded;charset=utf-8"); list<namevaluepair> entity = new arraylist<namevaluepair>(); entity.add(new basicnamevaluepair("name", params)); entity.add(new basicnamevaluepair("pass", params2)); try { httppost.setentity(new urlencodedformentity(entity)); } catch (unsupportedencodingexception e) { // todo auto-generated catch block e.printstacktrace(); } // execute http post request /* try { return httpclient.execute(httppost); } catch (clientprotocolexception e) { // todo auto-generated catch block e.printstacktrace(); return null; } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); return null; } */ return httpclient.execute(httppost); }
i tried code , return http 200 code. it's mean code works , application connected server. when want show data, using code:
public jsonobject getjsonfromurlauth(string url) { // making http request try { // defaulthttpclient config c = new config(); jsonobject jo = c.getsetting(); defaulthttpclient httpclient = new defaulthttpclient(); //credentialsprovider credprovider = new basiccredentialsprovider(); try { //credprovider.setcredentials(new authscope(authscope.any_host, authscope.any_port), new usernamepasswordcredentials(jo.getstring("username"), jo.getstring("password"))); //httpclient.setcredentialsprovider(credprovider); httpclient.getcredentialsprovider().setcredentials(new authscope(authscope.any_host, authscope.any_port), new usernamepasswordcredentials(jo.getstring("username"), jo.getstring("password"))); } catch (jsonexception e1) { // todo auto-generated catch block e1.printstacktrace(); } httppost httppost = new httppost(url); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } try { bufferedreader reader = new bufferedreader(new inputstreamreader( is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); json = sb.tostring(); } catch (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } // try parse string json object try { jobj = new jsonobject(json); } catch (jsonexception e) { log.e("json parser", "error parsing data " + e.tostring()); } // return json string return jobj; }
it return http 401 unauthorized.
my question why application can't connect server have been login?? how solve problem?? before
Comments
Post a Comment