Android 上的 Restlet 客户端:无法启动:(

发布于 2024-11-09 04:31:33 字数 2221 浏览 0 评论 0原文

我一直在玩 Restlet,到目前为止我很喜欢它:)

不过,我在让 Restlet 客户端在 Android 模拟器上工作时遇到了一些麻烦。

现在有一个非常简单的 Restlet 服务器在 JSE 下运行。它所做的只是通过 GET 方法公开根资源(目前只是一行文本)。我可以使用浏览器以及在控制台中运行的简单 Restlet 客户端来使用资源。我已经修改了 JSE 客户端的代码以尝试从 Android 模拟器连接,但无法使其工作:

package mypackage;

import android.app.Activity;
import android.os.Bundle;

import android.widget.TextView;

import org.restlet.resource.ClientResource;
import org.restlet.representation.Representation;

public class MyActivity extends Activity {

  @Override
  public void onCreate( Bundle savedInstanceState ) {

  super.onCreate( savedInstanceState );

    ClientResource res = new ClientResource( "http://server-ip:8111/" );
    Representation rep = res.get();

    TextView tv = new TextView( this );
    try {
      tv.setText( rep.getText() );
    } catch ( Exception ex ) {
      tv.setText( "Exception: " + ex.getMessage() );
    }
    setContentView( tv );

  }

}

注意:

(1) Android 模拟器上的内置浏览器可以连接到 Web 服务并检索文本。

(2) 项目Manifest.xml文件已被修改以启用使用权限INTERNET。

(3) 以下使用与 android 捆绑在一起的 Apache HttpClient 库的客户端工作得很好:

package mypackage;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

public class TestActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet( "http://server-ip:8111/" );
        ResponseHandler<String> handler = new BasicResponseHandler();

        TextView tv = new TextView( this );

        try {
            String content = client.execute( get, handler );
            client.getConnectionManager().shutdown();
            setContentView( tv );
            tv.setText( content );
        } catch ( Exception ex ) {
            tv.setText( "Exception: " + ex.getMessage() );
        }

    }

}

有什么想法我可能做错了吗?

预先感谢,

迈克。

I've been playing around a bit with restlet, and so far I like it :)

I am having some trouble getting a restlet client to work on an android emulator, though.

Right now a have a very simple restlet server running under JSE. All it does is expose a root resource (which for the moment is just a line of text) via the GET method. I can get consume the resource with a browser, and also with a simple restlet client running in the console. I have modified the code for the JSE client to try to connect from an android emulator but I can't get it to work:

package mypackage;

import android.app.Activity;
import android.os.Bundle;

import android.widget.TextView;

import org.restlet.resource.ClientResource;
import org.restlet.representation.Representation;

public class MyActivity extends Activity {

  @Override
  public void onCreate( Bundle savedInstanceState ) {

  super.onCreate( savedInstanceState );

    ClientResource res = new ClientResource( "http://server-ip:8111/" );
    Representation rep = res.get();

    TextView tv = new TextView( this );
    try {
      tv.setText( rep.getText() );
    } catch ( Exception ex ) {
      tv.setText( "Exception: " + ex.getMessage() );
    }
    setContentView( tv );

  }

}

Notes:

(1) The built-in browser on the android emulator can connect to the web service and retrieve the text.

(2) The project Manifest.xml file has been modified to enable the uses-permission INTERNET.

(3) The following client using the Apache HttpClient library that comes bundled with android works just fine:

package mypackage;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

public class TestActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet( "http://server-ip:8111/" );
        ResponseHandler<String> handler = new BasicResponseHandler();

        TextView tv = new TextView( this );

        try {
            String content = client.execute( get, handler );
            client.getConnectionManager().shutdown();
            setContentView( tv );
            tv.setText( content );
        } catch ( Exception ex ) {
            tv.setText( "Exception: " + ex.getMessage() );
        }

    }

}

Any ideas what I might be doing wrong?

Thanks in advance,

Mike.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对岸观火 2024-11-16 04:31:33

一种解决方法是手动注册 org.restlet.ext.httpclient 扩展以替换内部/默认 HTTP 客户端。请参阅 Restlet 用户指南:
http://wiki.restlet.org/docs_2 .1/13-restlet/275-restlet/266-restlet.html

最近的 Restlet 2.1 快照还修复了内部 HTTP 连接器长期存在的问题,该问题可能导致意外超时。

One workaround is to register the org.restlet.ext.httpclient extension manually to replace the internal/default HTTP client. See Restlet user guide:
http://wiki.restlet.org/docs_2.1/13-restlet/275-restlet/266-restlet.html

Recent Restlet 2.1 snapshots have also fixed a long standing issue with the internal HTTP connector that could cause unexpected timeouts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文