views:

59

answers:

3

Im looking for the concept of creating Java Server to handle Client requests and respond to it , i want to use Google App engine which does not allow Socket connections , so is the client & server in this case will communicate using Http requests? i'll be glad if someone could clarify the logic to me and provide a few lines of code.

Thanks

A: 

The Simple Framework may offer what you're looking for. It allows you to embed an HTTP server into your app with relatively little overhead:

import org.simpleframework.http.core.Container;
import org.simpleframework.transport.connect.Connection;
import org.simpleframework.transport.connect.SocketConnection;
import org.simpleframework.http.Response;
import org.simpleframework.http.Request;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.io.PrintStream;

public class HelloWorld implements Container {

   public void handle(Request request, Response response) {
      PrintStream body = response.getPrintStream();
      long time = System.currentTimeMillis();

      response.set("Content-Type", "text/plain");
      response.set("Server", "HelloWorld/1.0 (Simple 4.0)");
      response.setDate("Date", time);
      response.setDate("Last-Modified", time);

      body.println("Hello World");
      body.close();
   } 

   public static void main(String[] list) throws Exception {
      Container container = new HelloWorld();
      Connection connection = new SocketConnection(container);
      SocketAddress address = new InetSocketAddress(8080);

      connection.connect(address);
   }
}

To compare with other solutions, note that Simple is not only embeddable, but open source, fully self-contained and asynchronous throughout. Good luck!

Joe
Can you explain where i could add the server URL and handling the response . thanks
Fevos
Well, you were asking about "creating Java Server to handle Client requests and respond to it" -- but you couldn't use raw sockets. The Simple Framework allows you to embed an HTTP webserver within your application. So, the example above isn't about connecting to a remote server, but about allowing clients to connect to you (as your question seems to imply.) To be clear, I'm not sure how it would integrate with Google App engine.
Joe
A: 

Here is some sample code that may help. It uses app engine and google via HTTP requests.

Hamy
A: 

Thanks for all the answers but i needed a simple methods to use it in Android App Like the following code :

HTTP GET

try { HttpClient client = new DefaultHttpClient();
String getURL = "http://www.google.com"; HttpGet get = new HttpGet(getURL); HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null) {
//do something with the response Log.i("GET RESPONSE",EntityUtils.toString(resEntityGet)); } } catch (Exception e) { e.printStackTrace(); }

HTTP POST

try {
    HttpClient client = new DefaultHttpClient();  
    String postURL = "http://somepostaddress.com";
    HttpPost post = new HttpPost(postURL); 
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("user", "kris"));
        params.add(new BasicNameValuePair("pass", "xyz"));
        UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
        post.setEntity(ent);
        HttpResponse responsePOST = client.execute(post);  
        HttpEntity resEntity = responsePOST.getEntity();  
        if (resEntity != null) {    
            Log.i("RESPONSE",EntityUtils.toString(resEntity));
        }
} catch (Exception e) {
    e.printStackTrace();
}

The code is from this site and you dont need any additional Jar files to use it in Android and i manged to use it with Google App engine.

Fevos