简体   繁体   English

如何在Android环境中使用泽西客户端API?

[英]How to use jersey client api in an android context?

I´m trying to use my webservice via the jersey client api. 我正在尝试通过jersey客户端api使用我的网络服务。 This is my webservice: 这是我的网络服务:

@Path("/myresource")
@Component
@Scope("request")
public class MyResource {

@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("test")
public String test(){
     return "test";
} 

} }

This is my Client: 这是我的客户:

public class MyClient {
public static void main(String[] args) {
 System.out.println(Client.create().resource( "http://testwebservice.de:8080/CompetenceNetwork/api/myresource/test" ).get( String.class ) );

}

} }

Everythings works fine. 一切正常。 Now I try this in an android class: 现在,我在android类中尝试此操作:

 TextView tv;
 public void onCreate(Bundle savedInstanceState)
 {
      super.onCreate(savedInstanceState);
      tv = new TextView(this);
      Client c = Client.create();
      WebResource r = c.resource("http://testwebservice.de:8080/CompetenceNetwork/api/myresource/test");
      String s = r.get(String.class);
      tv.setText(s);
      setContentView(tv);
   }

Nothing happends and after a few minutes I get the following exception: 什么都没发生,几分钟后,我得到以下异常:

11-04 17:13:27.382: ERROR/AndroidRuntime(231): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tablayout/com.example.tablayout.MyLogin}: com.sun.jersey.api.client.ClientHandlerException: java.net.SocketException: The operation timed out
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2335)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:648)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at android.widget.TabHost.setCurrentTab(TabHost.java:320)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:379)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at android.view.View.performClick(View.java:2364)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at android.view.View.onTouchEvent(View.java:4179)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at android.view.View.dispatchTouchEvent(View.java:3709)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:852)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at android.os.Looper.loop(Looper.java:123)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at android.app.ActivityThread.main(ActivityThread.java:4363)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at java.lang.reflect.Method.invoke(Method.java:521)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at dalvik.system.NativeStart.main(Native Method)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): Caused by: com.sun.jersey.api.client.ClientHandlerException: java.net.SocketException: The operation timed out
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:131)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at com.sun.jersey.api.client.Client.handle(Client.java:616)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at com.sun.jersey.api.client.WebResource.handle(WebResource.java:559)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at com.sun.jersey.api.client.WebResource.get(WebResource.java:182)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at com.example.tablayout.MyLogin.onCreate(MyLogin.java:94)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): ... 31 more
11-04 17:13:27.382: ERROR/AndroidRuntime(231): Caused by: java.net.SocketException: The operation timed out
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at org.apache.harmony.luni.platform.OSNetworkSystem.connectSocketImpl(Native Method)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at org.apache.harmony.luni.platform.OSNetworkSystem.connect(OSNetworkSystem.java:114)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:245)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:535)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at java.net.Socket.connect(Socket.java:1054)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:62)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionManager$ConnectionPool.getHttpConnection(HttpConnectionManager.java:145)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionManager.getConnection(HttpConnectionManager.java:67)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getHTTPConnection(HttpURLConnection.java:821)
11-04 17:13:27.382: ERROR/AndroidRuntime(231): at org.apache.harmony.luni.internal.net.www.protocol.ht

I´m using target GoogleAPI 7 and the following jars: 我正在使用目标GoogleAPI 7和以下jar:

jersey-bundle-1.4.jar jsr311-api-1.1.jar junit-4.5.jar 泽西捆绑包1.4.jar jsr311-api-1.1.jar junit-4.5.jar

Does anybode has an idea to fix this? 有任何想法解决这个问题吗?

Greetings Christine Bauers 问候克里斯汀·鲍尔斯

do you have the proper permissions set in your manifest.xml so that your app can use internet? 您在manifest.xml中设置了适当的权限,以便您的应用程序可以使用互联网吗? I don't know the exact code but it should be easy enough to google it if you don't know it off the top of your head. 我不知道确切的代码,但是如果您不了解它,它应该很容易用谷歌搜索。 Make sure to put it at the same level as application, NOT inside of it, and it seems to work better if you put it BEFORE the application tag as well. 确保将它放在与应用程序相同的级别上,而不是放在其内部,并且如果将它放在application标记之前也似乎会更好。

将清单文件放在应用程序节点之外

It looks to me that you don't have your INTERNET permissions set in the android app. 在我看来,您没有在android应用中设置INTERNET权限。 Without it, the android VM would prevent you from communicating outside the phone. 没有它,Android VM将阻止您在电话外进行通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM