开发者

How to use jersey client api in an android context?

I´m trying to use my webservice via the jersey client 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:

 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.clien开发者_如何学运维t.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:

jersey-bundle-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? 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.


put in your manifest.xml outsided the application node


It looks to me that you don't have your INTERNET permissions set in the android app. Without it, the android VM would prevent you from communicating outside the phone.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜