Home Ask Login Register

Developers Planet

Your answer is one click away!

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 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();

        if(requestmethod == POST){
        }else if(requestmethod == GET){

        if(params != null){
            OutputStream outputstream = urlconnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(
                    new OutputStreamWriter(outputstream, "UTF-8"));

            StringBuilder result = new Strin        


Stallion February 2016

Use either AsyncTask or Thread for this functionality


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

        protected void onPostExecute(String result) {

        protected void onPreExecute() {

        protected void onProgressUpdate(Void... values) {

Or else normal Thread class

class ThreadFeature implements Runnable{
        public void run() {
            //Background operations you need to do

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


Leave an answer

Quote of the day: live life