开发者

Jersey WebServer for testing an Android App - got stuck at "hello world"

开发者 https://www.devze.com 2023-02-19 02:04 出处:网络
I am developing an Android App that gets some data from a web server, which I (or rather someone else) also create myself. Now, just for testing, I wanted to set up a simple Jersey Web Service that I

I am developing an Android App that gets some data from a web server, which I (or rather someone else) also create myself. Now, just for testing, I wanted to set up a simple Jersey Web Service that I can access via "httpGet". What I aim to achieve is to send some ?test request and get a "test!" String back. However, I have never worked with Web Servers before, so I feel kind of lost atm.

I followed a German "Hello World开发者_JS百科" example (it's supposed to show "Yeah" when calling the site localhost:8080/rest/message from the browser).

That's how my code looks:

import java.io.IOException;
import java.net.InetAddress;

import javax.swing.JOptionPane;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.ext.Provider;

import com.sun.jersey.api.container.httpserver.HttpServerFactory;
import com.sun.net.httpserver.HttpServer;

@Path("test")
@Provider
public class Test
{
    private static InetAddress adress;

    public static void main(String[] args)
    {   
        try
        {
            publishSite();
        }
        catch (IllegalArgumentException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        System.out.println("Der WebServer wurde unter der Adresse \"http://" + adress.getHostAddress() + ":8080/test\" veröffentlicht.");
    }

    public static void publishSite() throws IllegalArgumentException, IOException
    {
        adress = InetAddress.getLocalHost();
        HttpServer server = HttpServerFactory.create("http://" + adress.getHostAddress() + ":8080/");
        server.start();

        JOptionPane.showMessageDialog( null, "Ende" );
        server.stop( 0 );
    }


}

(Here I got some errors saying "Access restriction: The type HttpServer is not accessible due to restriction on required library C:\Programme\Java\jre6\lib\rt.jar" which I changed into warnings following some other website's guide. Bad idea?)

and, from the example mentioned above:

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Path("message")
public class Message {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String message() {
        return "Yeah! ";
    }
}

Now, if I try to access localhost:8080/test/message, I get an empty page. The same with localhost:8080/test. BUT, also the same with any random URL I can think of, like localhost:8080/blblab - which seems a bit weird to me.

Trying to access it from my Android App, I get an 404 error on localhost:8080/test/message and an 405 error on localhost:8080/test.

If it helps, I will post the Android code as well; but I have tested it on Google before and got some html string back so I think it's not the cause of my problem.

Hope you can help and also that my question isn't too stupid. :) Thanks in advance!

P.S.: please add "http://" to all the local links above in your mind, I am not allowed to post more than 2 links. ^^


Ok, it WAS a stupid question! ^^ It worked fine once I looked at localhost:8080/message instead of localhost:8080/test/message...

However, if anybody knows why I don't get an 404 response in my browser, I am still happy for another answer!

Also, with the "Access restriction" error, I found an easy solution here, in tgigiel's post.

0

精彩评论

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