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
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!
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.