android - How to solve the code with HttpUrlConnection and all new for api 23 -
i trying make login check sever have found code internet httpclient , other deprecated api 23 , higher. unable modify httpurlconnection,libray , updates.
main activity:
package com.example.login; import java.util.arraylist; import org.apache.http.namevaluepair; import org.apache.http.message.basicnamevaluepair; import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; public class loginlayout extends activity { edittext un,pw; textview error; button ok; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); un=(edittext)findviewbyid(r.id.et_un); pw=(edittext)findviewbyid(r.id.et_pw); ok=(button)findviewbyid(r.id.btn_login); error=(textview)findviewbyid(r.id.tv_error); ok.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub arraylist<namevaluepair> postparameters = new arraylist<namevaluepair>(); postparameters.add(new basicnamevaluepair("username", un.gettext().tostring())); postparameters.add(new basicnamevaluepair("password", pw.gettext().tostring())); string response = null; try { response = customhttpclient.executehttppost("<target page url>", postparameters); string res=response.tostring(); res= res.replaceall("\\s+",""); if(res.equals("1")) error.settext("correct username or password"); else error.settext("sorry!! incorrect username or password"); } catch (exception e) { un.settext(e.tostring()); } } }); } }
customclient class:
import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.net.uri; import java.util.arraylist; import org.apache.http.httpresponse; import org.apache.http.namevaluepair; import org.apache.http.client.httpclient; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.httpget; import org.apache.http.client.methods.httppost; import org.apache.http.conn.params.connmanagerparams; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.params.httpconnectionparams; import org.apache.http.params.httpparams; public class customhttpclient { /** time takes our client timeout */ public static final int http_timeout = 30 * 1000; // milliseconds /** single instance of our httpclient */ private static httpclient mhttpclient; /** * our single instance of our httpclient object. * * @return httpclient object connection parameters set */ private static httpclient gethttpclient() { if (mhttpclient == null) { mhttpclient = new defaulthttpclient(); final httpparams params = mhttpclient.getparams(); httpconnectionparams.setconnectiontimeout(params, http_timeout); httpconnectionparams.setsotimeout(params, http_timeout); connmanagerparams.settimeout(params, http_timeout); } return mhttpclient; } /** * performs http post request specified url * specified parameters. * * @param url web address post request * @param postparameters parameters send via request * @return result of request * @throws exception */ public static string executehttppost(string url, arraylist<namevaluepair> postparameters) throws exception { bufferedreader in = null; try { httpclient client = gethttpclient(); httppost request = new httppost(url); urlencodedformentity formentity = new urlencodedformentity(postparameters); request.setentity(formentity); httpresponse response = client.execute(request); in = new bufferedreader(new inputstreamreader(response.getentity().getcontent())); stringbuffer sb = new stringbuffer(""); string line = ""; string nl = system.getproperty("line.separator"); while ((line = in.readline()) != null) { sb.append(line + nl); } in.close(); string result = sb.tostring(); return result; } { if (in != null) { try { in.close(); } catch (ioexception e) { e.printstacktrace(); } } } } /** * performs http request specified url. * * @param url web address post request * @return result of request * @throws exception */ public static string executehttpget(string url) throws exception { bufferedreader in = null; try { httpclient client = gethttpclient(); httpget request = new httpget(); request.seturi(new uri(url)); httpresponse response = client.execute(request); in = new bufferedreader(new inputstreamreader(response.getentity().getcontent())); stringbuffer sb = new stringbuffer(""); string line = ""; string nl = system.getproperty("line.separator"); while ((line = in.readline()) != null) { sb.append(line + nl); } in.close(); string result = sb.tostring(); return result; } { if (in != null) { try { in.close(); } catch (ioexception e) { e.printstacktrace(); } } } } }
while has been deprecated can still use adding gradle file:
android { compilesdkversion 23 buildtoolsversion "23.0.2" uselibrary 'org.apache.http.legacy' ... }
Comments
Post a Comment