如何采用了android访问路由器的IP? [英] How to access router IP using android?

查看:311
本文介绍了如何采用了android访问路由器的IP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何访问路由器的IP与Android?

How can I access router ip with android?

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://192.168.195.1:80");
Log.d("test",post.toString());


try {
    HttpResponse response = client.execute(post);
    Log.d("test",response.toString());
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(response.getEntity().getContent()));
    String line="";
    while((line=reader.readLine())!=null){
        Log.d("test", line);
    }
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

我这个code尝试,但我得到的连接日志拒绝错误消息。我认为,这是由于路由器的身份验证。我如何才能访问到路由器?

I am trying with this code but I am getting connection refused error message in log. I think this is due to router authentication. How can I get access to router?

推荐答案

使用循环J我做一个HTTPGET打开一个会话并保存Cookie,则响应分析得到的SessionID(在我的情况下,它要求发送SOAP命令到路由器)。传递AsyncHttpClient的istance第二种方法会话仍处于打开状态(但你可以定义一个AsyncHttpClient为Class对象,我不知道你的code是如何所著)和performe命令

Using loopj I make an HTTPGet to open a session and save the cookie, the response is parsed to get the sessionId (in my case it's required to send SOAP command to router). Passing the istance of AsyncHttpClient to the second method the session still open (but you can define an AsyncHttpClient as object of Class, I don't know how your code is writed) and performe the command

 public void doCommandToRouter(final String channel, final genericResponse result) {
        final AsyncHttpClient client = new AsyncHttpClient();
        client.get("http://" + Constants.WIFICHANNEL.VS_DEFAULT_IP, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] response) {
                String session_id = parseSessionId(new String(response));
                applyChanneltoStation(client,session_id,channel,result);
            }
            @Override
            public void onFailure(int i, Header[] headers, byte[] response, Throwable throwable) {
                Log.d(TAG, "Fail");
                result.onError(i);
            }
        });
}
private void applyCommandtoRouter(final AsyncHttpClient client, final String session_id, final String bestChannel, final genericResponse result) {

    numTestChange = 0;
    String  xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">... SOAP COMMAND ...</soapenv:Envelope>";

    HttpEntity entity;
    try {
        entity = new StringEntity(xml, "UTF-8");
    } catch (IllegalArgumentException e) {
        Log.d("HTTP", "StringEntity: IllegalArgumentException");
        return;
    } catch (UnsupportedEncodingException e) {
        Log.d("HTTP", "StringEntity: UnsupportedEncodingException");
        return;
    }
    String  contentType = "text/xml";

    client.post( context, Constants.WIFICHANNEL.VS_SERVICE_URL, entity, contentType, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int i, Header[] headers, byte[] bytes) {
            Log.d(TAG, "post ok");
            //Log.d(TAG, "Response: " + new String(bytes));
            result.onSuccess();
        }

        @Override
        public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
            Log.d(TAG, "Fail. Error " + i);
        }

    });
}

要解析响应

    private String parseSessionId(String pageText) {
    String pattern = "(dm_cookie=')(.+)(';)";
    Pattern p  = Pattern.compile(pattern);
    Matcher m = p.matcher(pageText);
    String session_id = "";
    while(m.find()) {
        session_id = m.group(2);
        Log.d(TAG, "Found session id: " + session_id);
    }
    return session_id;
}

请记住,这只有在我的路由器,你必须检查你的路由器是如何工作的,因为这很可能与CTRL + C crtrl + V犯规的工作。

Please keep in mind that this works only on my router, you must check how your router works because probably this with ctrl+c crtrl+v doesnt' work.

这篇关于如何采用了android访问路由器的IP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆