java - Want to login to Router with android application -


i'm starting develop android application , want try connect router. wrote application using jackson, stuck json request login router. ip of router 192.168.1.1 , username , password should taken edittext fields. android.os.networkonmainthreadexception

there json string login:

{ "jsonrpc": "2.0", "id": 1, "method": "call", "params": [ "00000000000000000000000000000000", "session", "login", { "username": "root", "password": "admin01" } ] }

this restrequest.java

public class restrequest {  private static final string tag = jacksonutil.class.getsimplename(); static string response = null; public final static int = 1; public final static int post = 2;  //constructor no parameter public restrequest(){  }  public string makewebservicecall(string url, int requestmethod){     return this.makewebservicecall(url, requestmethod, null); }  public string makewebservicecall(string urladdress, int requestmethod, hashmap<string, string> params){      url url;     string responce = "";     try {         url = new url(urladdress);          httpurlconnection urlconnection = (httpurlconnection) url.openconnection();         urlconnection.setreadtimeout(15000);         urlconnection.setconnecttimeout(15000);         urlconnection.setdoinput(true);         urlconnection.setdooutput(true);          if(requestmethod == post){             urlconnection.setrequestmethod("post");         }else if(requestmethod == get){             urlconnection.setrequestmethod("get");         }          if(params != null){             outputstream outputstream = urlconnection.getoutputstream();             bufferedwriter bufferedwriter = new bufferedwriter(                     new outputstreamwriter(outputstream, "utf-8"));              stringbuilder result = new stringbuilder();             boolean first = true;             for(map.entry<string, string> entry : params.entryset()){                 if (first)                     first = false;                 else                     result.append("&");                  result.append(urlencoder.encode(entry.getkey(), "utf-8"));                 result.append("=");                 result.append(urlencoder.encode(entry.getvalue(), "utf-8"));             }              bufferedwriter.write(result.tostring());              bufferedwriter.flush();             bufferedwriter.close();             outputstream.close();         }          int responcecode = urlconnection.getresponsecode();          if(responcecode == httpsurlconnection.http_ok){             string line;             bufferedreader br = new bufferedreader(new inputstreamreader(urlconnection.getinputstream()));             while ((line = br.readline()) != null){                 responce += line;             }         }else {             responce = "";         }     } catch (exception e){         log.e(tag, "ioexception parsing json", e);     }      return response; } 

edited loginactivity.java can @ activity , tell i'm doing wrong

public class loginactivity extends appcompatactivity {  private string page_url = "http://192.168.1.1/ubus";  @bind(r.id.user_name_edit_text) textview user_name;  @bind(r.id.user_password_edit_text) textview user_password;  @bind(r.id.login_button) button login_button;   @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_login); butterknife.bind(this);  login_button.setonclicklistener(new view.onclicklistener() {      @override     public void onclick(view view) {          string url = page_url;         string username = getusername();         string password = getpassword();          trytologin trytologin = new trytologin();         trytologin.execute(url, username, password);     } });   class trytologin extends asynctask<string, void, void>{  @override protected void doinbackground(string... params) {      url url = null;     try {         url = new url(page_url);     } catch (malformedurlexception e) {         e.printstacktrace();     }     urlconnection connection = null;     try {         connection = url.openconnection();     } catch (ioexception e) {         e.printstacktrace();     }     try {         connection.connect();     } catch (ioexception e) {         e.printstacktrace();     }      return null; }  public string getusername() {     string username = user_name.gettext().tostring();     return username; }  private string getpassword() {     string password = user_password.gettext().tostring();     return password; } 

you cannot make network calls using main thread in android - design ensure ui thread remains responsive , not wait. need perform call in asyncthread or normal thread


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -