【问题标题】:Android/Apache HTTPGET Will not work, but in browser it doesAndroid/Apache HTTPGET 不起作用,但在浏览器中可以
【发布时间】:2015-07-10 23:19:03
【问题描述】:

我有一个在 tomcat 上运行的简单 servlet。它实现了一个简单的 doGet() 并在我从 Web 浏览器调用它时工作,例如:

http://192.168.1.140:8080/Application/doTask?id=0&param=something

但是,我试图从一个 android 应用程序中调用它,但除了挂起之外我什么也得不到。我在 AsyncTask 中运行它,所以它并不是真正的挂起,但我在“SendServerThread”中有这个

public SendServerThread(String state, int isHalfway) {
    mState = state;
    mIsHalfWay = mIsHalfWay;

    mRestClient = new RestClient();
}

@Override
protected String doInBackground(String... params) {
    HttpClient httpClient = new DefaultHttpClient();
    String response = "";
    try {
        URI urlString = new URI(PI_ADDRESS + "/Application/API?id=0&setState=" + mState + "&percentage=" + mIsHalfWay);
        HttpGet httpGetRequest = new HttpGet();
        httpGetRequest.setURI(urlString);

        HttpResponse httpResponse = httpClient.execute(httpGetRequest);

        String line = "";
        BufferedReader br = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
        while ((line = br.readLine()) != null) {
            response+=line;
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        httpClient.getConnectionManager().shutdown();
    }

    return response;
}

我验证了它调用的 URL 是正确的,但我什么也没得到。没有反应。我有 INTERNET 权限。

我从 UI 中这样称呼它:

            new SendServerThread(state, isHalfWay)
            {
                @Override public void onPostExecute(String result)
                {
                    unlockUI();
                }
            }.execute("");

它通过 HttpGet,但线程从未得到响应。如果我进入手机上的网络浏览器,我会得到结果。但是从应用程序我什么也没得到 - 线程挂起。

任何想法都非常感谢,无法弄清楚我做错了什么

【问题讨论】:

  • 做两件事并检查...首先删除端口号。 8080 ...如果它不起作用...然后使用10.0.2.2而不是192.168.1.140:8080

标签: java android rest tomcat servlets


【解决方案1】:

我打开了飞行模式,然后打开了 WiFi,所以所有的移动数据都被禁用了,然后它就可以工作了 :(

【讨论】:

  • 您也应该使用设备上的浏览器进行检查。
【解决方案2】:

如果您使用模拟器进行测试,则 localhost 的 IP 地址为 10.0.2.2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 2016-11-21
    • 2012-06-12
    • 1970-01-01
    • 2014-08-13
    相关资源
    最近更新 更多