Skip to content

Instantly share code, notes, and snippets.

@dhavaln
Created March 14, 2012 20:32
Show Gist options
  • Save dhavaln/2039298 to your computer and use it in GitHub Desktop.
Save dhavaln/2039298 to your computer and use it in GitHub Desktop.
Phonegap v1.5 Network Test
<html>
<head>
<title>Network Test</title>
<script src="js/jquery.js"></script>
</head>
<body>
<button id="loadData">Load Data</button>
<script type="text/javascript">
$("#loadData").click(function(){
console.log("calling service");
remoteGet('http://192.168.1.103:45759/test_service/service1?id=1111&callback=?', function(data){
console.log(JSON.stringify(data));
}, function(e){
console.log(JSON.stringify(e));
});
})
function remoteGet(url, success, failure) {
var settings = {
url: url,
dataType: 'json',
success: success,
timeout: 5000,
error: failure
};
$.ajax(settings);
}
/**
* My server returns the data in following way so this function is mandatory
* jsonpcallback({"status":"success", "message": "print this thing"})
*/
function jsonpcallback(data){
console.log(" in jsoncallback " + JSON.stringify(data));
}
</script>
</body>
</html>
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
public class TestService extends HttpServlet{
Gson gson = new Gson();
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException{
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException{
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
Map<String, String> respResult = new HashMap<String, String>();
respResult.put("status", "success");
respResult.put("message", "this is a test message");
PrintWriter pw = new PrintWriter(response.getOutputStream());
pw.write("jsonpcallback(" + gson.toJson(respResult) + ")");
pw.flush();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment