Skip to main content

Posts

Showing posts with the label http

Asynchronous HTTP Client for Java

As a relative newbie in the Java world, I am finding many things frustratingly obtuse to accomplish that are relatively trivial in many other frameworks. A primary example is a simple solution for asynchronous http requests. Seeing as one doesn't seem to already exist, what is the best approach? Creating my own threads using a blocking type lib like httpclient or the built-in java http stuff, or should I use the newer non-blocking io java stuff - it seems particularly complex for something which should be simple.

Cannot properly set the Accept HTTP header with jQuery

I'm trying to set the Accept HTTP header to "text/xml" with this jquery code: $.ajax({ beforeSend: function(req) { req.setRequestHeader("Accept", "text/xml"); }, type: "GET", url: "[proper url]", contentType: "text/plain; charset=utf-8", dataType: ($.browser.msie) ? "text" : "xml", username: '---', password: '-------', success: function(data) { var xml; if (typeof data == "string") { alert("Data is string:" + data); xml = new ActiveXObject("Microsoft.XMLDOM"); xml.async = false; xml.loadXML(data); } else { xml = data; alert("Data is not string:" + $(xml).text()); } // Returned data available in object "xml" //alert("Status is: " + xml.

Update whole page on Ajax request

I have an AJAX request that can have two possible outcomes: The server responds with a message which I should place in a <div> The server responds with an HTML page, in this case I need to substitute current page with a new one and change the address (the client knows the address before a request). What would be the solution if I have the AJAX request that needs to handle both of these cases? url = "http://example.com" ajax.request(callback) function callback(response) { if (case2(response)) { history.pushState({}, "New page", url); document.innerHTML = response } else { updateDiv(response) } } I'm interested in a correct way to implement the first branch, or if the server can somehow compose a headers that will make browser to handle a response as a usual HTTP response and update a page location and content, something like redirect with given content. I understand that the server can retu

Send data from android to mysql using php

I have a some text on an android client, I want to send it to the database(MySQL). How do I do this.Please help me with this. I tried using php and Mysql. Is the query in Php right?? Here is what I have tried Insert.java public class Insert extends ListActivity { String[] ct_name = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.main); InputStream is = null; // http post ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("c_name","KL")); try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://10.0.2.2/city1.php"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = enti

Send data from android to mysql using php

I have a some text on an android client, I want to send it to the database(MySQL). How do I do this.Please help me with this. I tried using php and Mysql. Is the query in Php right?? Here is what I have tried Insert.java public class Insert extends ListActivity { String[] ct_name = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.main); InputStream is = null; // http post ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("c_name","KL")); try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://10.0.2.2/city1.php"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = enti

Jersey Client: Cache-Manager for Conditional GET?

Jersey has wonderful support for server-side handling of Preconditions to respond to a Conditional-GET-request. On the client-side it seems a bit less elegant/manual. As far as I know you'd need to store the metadata of the entity yourself (etag, last-modified header) and make a decision, when to set which headers, evaluate the response code, update your local cache of entity/metadata ... etc. Do you know of a ready, free implementation that wraps up the conditonal GET? I found this example , where the poster is using CacheManager and CacheEntry ; I suspect his own implementation. Shouldn't this be part of Jersey Client itself?

ThreadPoolExecutor for running AbortableHttpRequest - how to call abort?

I'm running a networking service in android where I direct all my http requests to run and get callbacks from the service when the requests are complete. I run the requests in a ThreadPoolExecutor to limit the number of concurrent requests. As the requests run within the pool, they eventually create an HttpGet or HttpPost, both of which indirectly implement AbortableHttpRequest , which allows one to cancel the connection (say, if it's blocking for a long time). If a user cancels a request, I'd like to somehow drill into the thread queue and call the abort routine for that request. If, for example, a web site is not responding and the user chooses to do something else, right now my only option is to wait for the standard 5 minute http timeout to occur for that hung request before that thread is freed up. If I could access the thread that has my request and call abort, that would free things up right away. From what I can understand, it appears once my request has gon

How to disable output buffering in PHP

I wrote a simple relay script that connects to a web camera and reads from the socket, and outputs this data using the print function. The data is MJPG data with boundaries already setup. I just output the data that is read. The problem is PHP seems to be buffering this data. When I set the camera to 1 FPS, the feed will freeze for 7-8 seconds, then quickly display 8 frames. If I set the resolution to a huge size, the camera move at more or less 1 frame per second. I assume then some buffering is happening (since huge sizes fill the buffer quickly, and low sizes don't), and I can't figure out how to disable this buffering. Does anyone know how to? Code: ignore_user_abort(false); $boundary = "myboundary"; //Set this so PHP doesn't timeout during a long stream set_time_limit(0); $socketConn = @fsockopen ("192.168.1.6", 1989, $errno, $errstr, 2); if (!$socketConn) exit(); stream_set_timeout($socketConn, 10); fputs ($socketConn, "GET /mjpeg HTTP/1