android - HttpPost is not working in andorid (with php and iis 8) -
i use code data server hosted on internet using httppost request
and use asynctask class call makehttprequest method in jsonparser class in following link
public class jsonparser { static inputstream = null; static jsonobject jobj = null; static string json = ""; public jsonobject makehttprequest(string url, string method, list<namevaluepair> params) { try { if (method == "post") { defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); urlencodedformentity urlencodedformentity = new urlencodedformentity( params); httppost.setentity(urlencodedformentity); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); } else if (method == "get") { defaulthttpclient httpclient = new defaulthttpclient(); string paramstring = urlencodedutils.format(params, "utf-8"); log.d("get_param", paramstring); url += "?" + paramstring; log.d("get_url", url); httpget httpget = new httpget(url); httpresponse httpresponse = httpclient.execute(httpget); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); } } catch (unsupportedencodingexception e) { e.printstacktrace(); log.d("unsupportedencodingexception", "unsupportedencodingexception"); } catch (clientprotocolexception e) { e.printstacktrace(); log.d("clientprotocolexception", "clientprotocolexception"); } catch (illegalstateexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block 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(); log.d("json_string", json.tostring()); } catch (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } try { jobj = new jsonobject(json); } catch (jsonexception e) { log.e("json parser", "error parsing data " + e.tostring()); } return jobj; }
}
call makehttprequest
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.login); url = (edittext) findviewbyid(r.id.url); t = (textview) findviewbyid(r.id.text); usernameedittext = (edittext) findviewbyid(r.id.login_username_edittext); passwordedittext = (edittext) findviewbyid(r.id.login_password_edittext); sendpostreqbutton = (button) findviewbyid(r.id.login_sendpostreq_button); sendpostreqbutton.setonclicklistener(this); } @override public void onclick(view v) { if (v.getid() == r.id.login_sendpostreq_button) { string givenusername = usernameedittext.geteditabletext() .tostring(); string givenpassword = passwordedittext.geteditabletext() .tostring(); system.out.println("given username :" + givenusername + " given password :" + givenpassword); string url = "http://xx.xx.xx.xx/login.php"; basicnamevaluepair usernamebasicnamevaluepair = new basicnamevaluepair("paramusername", paramusername); basicnamevaluepair passwordbasicnamevaluepair = new basicnamevaluepair( "parampassword", parampassword); list<namevaluepair> params = newarraylist<namevaluepair>(); params.add(usernamebasicnamevaluepair); params.add(passwordbasicnamevaluepair); jsonobject jsonobject = null; try { jsonobject = new async(customerdetails.this, url,"post") .execute(params).get(); } catch (interruptedexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (executionexception e1) { // todo auto-generated catch block e1.printstacktrace(); } try { string success = jsonobject.getstring("success"); if (success.equals("working") && success != null) { toast.make(this,""+success,toast.length_short).show(); } } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } } }
async task class
public class async extends asynctask<list<namevaluepair>, string, jsonobject> { progressdialog progressdialog; context c; string method, url; public submitasync(context c, string url, string method) { // todo auto-generated constructor stub this.c = c; this.method = method; this.url = url; } @override protected void onpostexecute(jsonobject result) { // todo auto-generated method stub progressdialog.dismiss(); super.onpostexecute(result); } @override protected void onpreexecute() { // todo auto-generated method stub super.onpreexecute(); progressdialog = new progressdialog(c); progressdialog.setmessage("uploading data..."); progressdialog.setindeterminate(true); progressdialog.show(); } @override protected jsonobject doinbackground(list<namevaluepair>... params) { // todo auto-generated method stub jsonparser jsonparser = new jsonparser(); jsonobject jsonobject = jsonparser.makehttprequest(url, method, params[0]); return jsonobject; }
}
uses permission
<uses-permission android:name="android.permission.internet" />
i user php file
<?php $varusername = $_post['paramusername']; $varpassword = $_post['parampassword']; $success = array(); if($varusername == "anuja" && $varpassword == "123"){ $success['success'] = 'working'; echo json_encode($success); }else{ $success['success'] = 'invalid'; echo json_encode($success); } ?>
it returning nothing.my emulator not responding @ time. when checked local wamp server worked when tried remote server not returning.
Comments
Post a Comment