JonasSeputis February 2016

Want to login to Router with android application

I'm just starting to develop android application and want to try to connect to my router. Wrote application using Jackson, but get stuck with json request for login to the router. IP of my router is 192.168.1.1 and username and password should be taken from EditText fields. android.os.NetworkOnMainThreadException

There is json string for login:

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

This is my RestRequest.java

public class RestRequest {

private static final String TAG = JacksonUtil.class.getSimpleName();
static String response = null;
public final static int GET = 1;
public final static int POST = 2;

//Constructor with 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 Strin        

Answers


Stallion February 2016

Use either AsyncTask or Thread for this functionality

AsyncTask:

private class LongOperation extends AsyncTask<Void, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            //Background operations you need to do
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
        }

        @Override
        protected void onPreExecute() {
        }

        @Override
        protected void onProgressUpdate(Void... values) {
        }
    }

Or else normal Thread class

class ThreadFeature implements Runnable{
        @Override
        public void run() {
            //Background operations you need to do
            makeWebServiceCall();
        }
    }


ucsunil February 2016

You cannot make network calls using the main thread in Android - this is by design to ensure the UI thread remains responsive and does not wait. You need to perform this call in an AsyncThread or normal Thread

Post Status

Asked in February 2016
Viewed 1,670 times
Voted 13
Answered 2 times

Search




Leave an answer