I'm running a Java code that listen to a long polling HTTP stream.
This is the method I'm using.
void connectStream() throws IOException, URISyntaxException {
URIBuilder uriBuilder = new URIBuilder("...");
HttpGet httpGet = new HttpGet(uriBuilder.build());
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (null != entity) {
InputStreamReader stream = new InputStreamReader((entity.getContent()));
BufferedReader reader = new BufferedReader(stream);
String line;
while ((line = reader.readLine()) != null) {
// do stuff
}
}
}
I need to interrupt gracefully this stream from the main thread. What is the best way to do it?
For now, I'm adding an AtomicBoolean variable and check it in each iteration of the loop.
private AtomicBoolean interrupt = new AtomicBoolean(false);
void connectStream() throws IOException, URISyntaxException {
URIBuilder uriBuilder = new URIBuilder("...");
HttpGet httpGet = new HttpGet(uriBuilder.build());
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (null != entity) {
InputStreamReader stream = new InputStreamReader((entity.getContent()));
BufferedReader reader = new BufferedReader(stream);
String line;
while ((line = reader.readLine()) != null) {
if (interrupt.get()) {
break;
}
// do stuff
}
}
}
public void setInterrupt() {
this.interrupt.set(true);
}
This works well when the buffer often contains data. But what if the buffer remains empty for a long time?
I've already tried to close stream and reader: the program does not execute the code inside the loop any more it does not exit from it.
Solved! I could find a way to get the socket underlying the HTTP connection. By closing it, the thread quits correctly.
// No need this anymore
// private AtomicBoolean interrupt = new AtomicBoolean(false);
// New
private Socket socket;
void connectStream() throws IOException, URISyntaxException {
URIBuilder uriBuilder = new URIBuilder("...");
HttpGet httpGet = new HttpGet(uriBuilder.build());
// New block code here
HttpClientContext context = HttpClientContext.create();
HttpResponse response = httpClient.execute(httpGet, context);
ManagedHttpClientConnection connection = context.getConnection(ManagedHttpClientConnection.class);
socket = connection.getSocket();
HttpEntity entity = response.getEntity();
if (null != entity) {
InputStreamReader stream = new InputStreamReader((entity.getContent()));
BufferedReader reader = new BufferedReader(stream);
String line;
while ((line = reader.readLine()) != null) {
// do stuff
}
}
}
public void setInterrupt() {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Related
first of all I have to say that I'm a beginner in Android programing and not very experienced in general programing at all. But now I decided to make a little app for my private use.
In my app I need to get some Text from a given URL into a string. I found some methods on the web and personalized them a bit. But there is something wrong because when I run the app with the Android Eclipse emulator it says "Unfortunately, xxx_app has stopped.".
Here is my code:
public String getURLtext(String zielurl) throws IllegalStateException, IOException{
String meineurl = zielurl;
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(meineurl);
HttpResponse response = httpClient.execute(httpGet, localContext);
String result = "";
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()
)
);
String line = null;
while ((line = reader.readLine()) != null){
result += line + "\n";
}
return result;
}
And this is the method where I want to show the output string in my EditText text1.
public void test(View view) throws InterruptedException, ExecutionException, IllegalStateException, IOException {
EditText text1 = (EditText)findViewById(R.id.textfeld);
String teststring = getURLtext("http://ephemeraltech.com/demo/android_tutorial20.php");
text1.setText(teststring);
}
I would be happy if anyone can help me with this.
Thanks!
Your code looks good until you get the HTTPResponse, the bottom part (response to string) can be optimized a lot. It's also worth noticing that Android won't let you to do network operation on the main thread, so please consider using an AsyncTask to execute your http GET operation.
public String getURLtext(String zielurl) throws IllegalStateException, IOException
{
String result = ""; // default empty string
try
{
String meineurl = zielurl;
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(meineurl);
HttpResponse response = httpClient.execute(httpGet, localContext);
InputStream is = response.getEntity().getContent();
result = inputStreamToString(is).toString();
}
catch (Exception ex)
{
// do some Log.e here
}
finally
{
return result;
}
}
// Fast Implementation
private StringBuilder inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// Read response until the end
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Return full string
return total;
}
What I'm trying to do is to generate a byte array from a url.
byte[] data = WebServiceClient.download(url);
The url returns json
public static byte[] download(String url) {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
try {
HttpResponse response = client.execute(get);
StatusLine status = response.getStatusLine();
int code = status.getStatusCode();
switch (code) {
case 200:
StringBuffer sb = new StringBuffer();
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
is.close();
sContent = sb.toString();
break;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sContent.getBytes();
}
This data is used as a parameter for String
String json = new String(data, "UTF-8");
JSONObject obj = new JSONObject(json);
for some reason, I get this error
I/global ( 631): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
I think something there must be missing here sContent = sb.toString(); or here return sContent.getBytes(); but I'm not sure though.
1. Consider using Apache commons-io to read the bytes from InputStream
InputStream is = entity.getContent();
try {
return IOUtils.toByteArray(is);
}finally{
is.close();
}
Currently you're unnecessarily converting the bytes to characters and back.
2. Avoid using String.getBytes() without passing the charset as a parameter. Instead use
String s = ...;
s.getBytes("utf-8")
As a whole I'd rewrite you're method like this:
public static byte[] download(String url) throws IOException {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse response = client.execute(get);
StatusLine status = response.getStatusLine();
int code = status.getStatusCode();
if(code != 200) {
throw new IOException(code+" response received.");
}
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
try {
return IOUtils.toByteArray(is);
}finally{
IOUtils.closeQuietly(is.close());
}
}
I'm aiming to make a very basic application which reads the HTML and stores it into a string. I'm only interested in one line from the website's source. I found a topic which suggested this:
String bodyHtml = "null";
try {
String myUri = "http://www.spring8.or.jp/ext/ja/status/text.html";
HttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet(myUri);
HttpResponse response = httpClient.execute(get);
// Build up result
bodyHtml = EntityUtils.toString(response.getEntity());
} catch (Exception e) {
}
url.setText(bodyHtml);
With url being my textview. I have set the permissions in the manifest correctly as far as I'm aware.
However when I run this code on my phone and the emulator, it doesn't seem to work at all. I get nothing. Am I missing something?
Thank you
Try this instead of the EntityUtils
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String line = "";
String newLine = "";
while ((line = rd.readLine()) != null) {
newLine = newLine.concat(line);
}
System.out.println(newLine);
Try this,
Call the below method to download the HTml Content and pass the Url in the parameter,
private void downloadText(String urlStr) {
progressDialog = ProgressDialog.show(this, "",
"Download Text from " + urlStr);
final String url = urlStr;
new Thread () {
public void run() {
int BUFFER_SIZE = 2000;
InputStream in = null;
Message msg = Message.obtain();
msg.what=1;
try {
in = openHttpConnection(url);
InputStreamReader isr = new InputStreamReader(in);
int charRead;
text = "";
char[] inputBuffer = new char[BUFFER_SIZE];
while ((charRead = isr.read(inputBuffer))>0)
{
String readString =
String.copyValueOf(inputBuffer, 0, charRead);
text += readString;
inputBuffer = new char[BUFFER_SIZE];
}
Bundle b = new Bundle();
b.putString("text", text);
msg.setData(b);
in.close();
}catch (IOException e2) {
e2.printStackTrace();
}
messageHandler.sendMessage(msg);
}
}.start();
}
This the helper method which returns InputStream Object,
private InputStream openHttpConnection(String urlStr) {
InputStream in = null;
int resCode = -1;
try {
URL url = new URL(urlStr);
URLConnection urlConn = url.openConnection();
if (!(urlConn instanceof HttpURLConnection)) {
throw new IOException ("URL is not an Http URL");
}
HttpURLConnection httpConn = (HttpURLConnection)urlConn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
resCode = httpConn.getResponseCode();
if (resCode == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return in;
}
And now display the String in a textView using Handler,
private Handler messageHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
TextView text = (TextView) findViewById(R.id.textview01);
text.setText(msg.getData().getString("text"));
break;
}
progressDialog.dismiss();
}
};
Provide the INTERNET permission in the manifest.
In the execute method of the HttpClient, also put a HttpContext as you see below:
HttpContext localContext = new BasicHttpContext();
HttpResponse response = httpClient.execute(get, localContext);
And also use a BufferedReader:
final BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
If it doesn't work, you'll probably have a problem with you Internet connection.
BTW, don't forget about the android.permission.INTERNET permission!
I am trying to read the buffer (android application) and set the value to my TextView 'httpStuff'. But i dont think i am getting some response from the URI.
I don't get any runtime errors. I tried many flavour of the same logic. Nothing seems to be working.
INTERNET permission is already set in the manifest. SdkVersion="15". Any help ?
HttpClient client = new DefaultHttpClient();
URI website = new URI("http://www.mybringback.com");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
httpStuf.setText( in.readLine());
I think you are missing the while loop and also, when you say only in.readLine(), may be it is returning you an empty line from the response, though it is having enough data.So make sure to read the reader entirely like this and check its contents.
while ((line = rd.readLine()) != null) {
httpStuf.setText(line+"\r\n");
}
Hope this will help you.
This code worked for me
InputStream is = response.getEntity().getContent();
String strResponse = inputStreamToString(is);
private String inputStreamToString(InputStream is)
{
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is), 1024 * 4);
// Read response until the end
try
{
while ((line = rd.readLine()) != null)
{
total.append(line);
}
} catch (IOException e)
{
Log.e(TAG, "error build string" + e.getMessage());
}
// Return full string
return total.toString();
}
try to get the status code of response and Then you can compare with the (HTTP status)
int responseCode=response.getStatusLine().getStatusCode()
I am using this method to simply catch the HTTP response and it works fine for me.
public String httpGetResponse(String url) {
try {
Log.i("HTTP Request", "httpGet Request for : " + url);
DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
//get.setHeader("Connection", "keep-alive");
HttpResponse response = client.execute(get);
InputStream is = response.getEntity().getContent();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(is));
StringBuilder str = new StringBuilder();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
str.append(line + "\n");
}
return str.toString();
} catch (Exception e) {
Log.e("HTTP error", "Error in function httpGetResponse : "
+ e.getMessage());
return null;
}
}
I try to get HTML content, everything works find except 1 thing. It doesn't download whole code and skip the content which I want to extract(urls to images, names) and I have just blank classes 'obrazek'.
Here is the code i use to get source code:
String SourceCode(String adres) throws IllegalStateException, IOException
{
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(adres);
HttpResponse response = null;
try {
response = httpClient.execute(httpGet, localContext);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader reader = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()
)
);
String result = "";
while(reader.readLine() != null)
{
result += reader.readLine();
}
reader.close();
return result;
Thank you for help:)
You skip one line each time. should be
StringBuilder result = new StringBuilder();
String line;
while((line = reader.readLine()) != null)
{
result.append(line);
}
reader.close();
return result.toString();
BTW - I used StringBuilder to avoid creation of new String object each iteration - very recommended.