I am using the following piece of code to perform an HTTP Request in Android. Everything seems fine, but it appears that the response gets clipped at some point and I can't read the full content of the requested url.
Is there any size limit on the response?
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(someUrl);
HttpResponse response = httpclient.execute(request);
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
String responseString = out.toString();
Log.w("Response", responseString); // The response is clipped at some point
}
Thank you.
Ok, silly me. Turns out this is a log problem. I checked out for size and the full content is there. The problem lies with the size limitation for LogCat, as explained in the following answer.
What is the size limit for Logcat and how to change its capacity?
Cheers
Related
I would like to get full content of website including some content loaded wich is depend of parameters in GET query. I have website www.example.com?date=2012-12-12. If I use it in any broswer first I see page with "waiting for serwer", after some miliseconds full content is loading. Now I would like to load content of this website after this some miliseconds in android. I don't know how to wait for full loading this.
private String makeHTTPRequest() throws IOException
{
String url = "http://www.centrum.saletyni.pl/index.php?option=com_content&view=article&id=18&data=2014-11-06";
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(url));
StatusLine statusLine = response.getStatusLine();
String responseString;
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
//..more logic
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
return responseString;
}
Sounds like you need a thread to execute after this other method is run, or as specified, after a certain amount of time.
e.g. You could use this for a specified time:
System.Threading.Thread.Sleep(10000);
http://msdn.microsoft.com/en-us/library/system.threading.thread(v=vs.110).aspx
I'm having some issues with incomplete post data being received by our server. This only occurs for a few users of our app. Still not sure which versions of Android we are talking about, but will try to recover that information as soon as possible.
For the time being: maybe there is someone who also experienced similar problems? It occurs only for our textarea field where users can type large custom texts. There is no filter whatsoever on this field, and will be sent as StringBody to our server directly. The code below is the relevant parts. My concern is the part where the StringBody is created. Is this sufficient for escaping the text properly to not interfere with the post headers?
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
for(int j=0; j<fields.length; j++){
String value = mydata.get(fields[j]);
entity.addPart(fields[j], new StringBody(value, "text/plain", Charset.forName("UTF-8")));
}
(..) //some files are added too later on
entity.addPart(image.get("hash"), new FileBody(imageFile));
(..) //and sending the data to the server
HttpPost post = new HttpPost(url);
if(entity != null)
post.setEntity(entity);
final HttpClient http = new DefaultHttpClient();
final HttpParams params = http.getParams();
HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 30000);
ConnManagerParams.setTimeout(params, 30000);
HttpResponse response = http.execute(post);
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() != HttpStatus.SC_OK){
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
String result = out.toString();
Finally solved this problem. The problem was not in the frontend but in the backend. People were posting unicode emoticons . The backend was built with an MySQL utf8_general_ci collation table/column. This collation does not support the unicode characters and will break the text before the unicode character. So the resulting insert will have all text missing after the unicode character.
I'm having a bit of a problem with decodeStream returning null. It seems to be a fairly common problem around, but it's usually pinned down to one of two problems:
An OutOfMemory exception thrown by attempting to load a large bitmap in it's entirety.
Attempting to use the same input stream twice.
However, I'm not doing either. The code to run it is simply
stream = new java.net.URL(url).openStream();
Bitmap image = BitmapFactory.decodeStream(stream);
stream.close();
with the URL set to here. image is null after this code is complete. This issue's been driving me completely insane - it works fine on PNGs but seems to fall apart under every BMP I can give it, so any help would be appreciated.
Ultimately, the answer was found here, using an InputStream returned by a BufferedHTTPEntity. While it seems needlessly complex, I can only assume that simply getting a stream from the URL object directly doesn't return a stream of the appropriate type, and so it wasn't reading out all the data properly.
Cross-posting the code in case the question is erased:
private static InputStream fetch(String address) throws MalformedURLException,IOException {
HttpGet httpRequest = new HttpGet(URI.create(address) );
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
InputStream instream = bufHttpEntity.getContent();
return instream;
}
I have the following site http://www.freewebservicesx.com/GoldSpotPrice.aspx which provides a webservice api. As i am extremely new to soap and rest i have absolutely no clue about how to call this service and use it in android. Could someone please tell me how to do it.
You can make HTTP requests using either HttpURLConnection or HttpClient. Example of using HttpClient for a RESTful web service:
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://www.mywebsite.com/webservice");
HttpResponse response = httpClient.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
ByteArrayOutputStream out = new ByteArrayOutputStream();
entity.writeTo(out);
out.close();
String responseStr = out.toString();
// process response
} else {
// handle bad response
}
Otherwise, if you're working with a SOAP web service, I would recommend using ksoap2
Use the following url to refer:::
Using ksoap2 for android, and parsing output data...
ksoap2-android - HowToUse.wiki.....
ksoap2-android
Use this LINK it has a sample the suits your requirement.
also LINK
I am trying to use Google graph API (image) to show some data in the form of PIE chart. http://chart.apis.google.com/chart?chs=250x150&cht=p3&chd=t:41.86,26.00,21.78,10.36&chdl=User998|User591|User671|Others, this link gives a pie chart when viewed in browser. But, when I am trying to get the response using HttpClient, I am getting illegal character error. I am using following code to get the response
HttpClient client = new DefaultHttpClient();
HttpResponse httpResponse;
try {
String chartUrl = "above url";
//Here, I am getting illegal character error.
HttpGet getRequest = new HttpGet(chartUrl);
getRequest.setHeader("Content-Type", "image/png");
httpResponse = client.execute(getRequest);
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
bmImg = BitmapFactory.decodeStream(instream);
instream.close();
}
}
catch(Exception e)
{
//TODO
}
Can anyone tell me how to fix this issue?
Thanks,
Ashwani
The character in question is |, you can work around by using %7C instead in the URL and it should work.