Illegal character error while using Google graph API in Android - android

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.

Related

javax.net.ssl.SSLPeerUnverifiedException: No peer certificate on HttpClient but works fine with HttpUrlConnection

Previously,I used HttpClient for a http post request and it was working fine, until I believe the server team made some changes. Then I kept getting
javax.net.ssl.SSLPeerUnverifiedException: No peer certificate Exception.
Then, after alot of scratching my head, I tried HttpUrlConnection and it works fine, but still I can't figure out why I got that exception while using HttpClient.
Before code was :
public String postDataAndGetStringResponse( List<NameValuePair> nameValuePairs ) {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost( link );
try {
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
InputStream is = response.getEntity().getContent();
String result = "";
if (is != null) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
String l = "";
while ((l = reader.readLine()) != null) {
result += l;
}
reader.close();
}
is.close();
return result;
} catch (Exception e) {
Logger.printStackTrace(e);
return ServerUnrechable;
}
}
I did check the server using https://www.sslshopper.com and everything is ticked, it would be very helpful if anybody could tell me the cause to this issue.
One of the most likely causes is that the server you're trying to use now relies on Server Name Indication.
SNI support was added a to HttpsURLConnection in Android, but not to the Apache HTTP Client bundled (now deprecated/removed). See this related question for details.

Android HTTPClient.execute stopped working after years

I have some Android applications that have not been touched in years. All of the sudden today they all stopped working. They all read data from a txt file that sits on an https:// site.
If I change the https:// call to http:// everything works fine again. I need the https:// for security. Can anyone tell me what happened?
Keep in mind that this application is a few years old. Did something change in an online library or something to break the https:// calls?
Here is the code. params[0] hold the website address. I have verified the address is correct. Just changing it to http:// fixes everything. II also know it is not the ssl certificate since I have verified it is all working.
It fails on the httpClient.execute command:
protected List<String> doInBackground(String... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httppost = new HttpGet(params[0]);
HttpResponse response;
List<String> data = new ArrayList<String>();
try {
response = httpClient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
InputStream is = buf.getContent();
BufferedReader r = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = r.readLine()) != null) {
data.add(line);
}
return data;
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
I don't have any logcat or console output that specifies what the error actually is. I could not find anything.
Thanks for any help.

Socket closed exception when trying to read httpResponse

I have a method to connect to send post data to a webservice and get the response back as follow:
public HttpResponse sendXMLToURL(String url, String xml, String httpClientInstanceName) throws IOException {
HttpResponse response = null;
AndroidHttpClient httpClient = AndroidHttpClient.newInstance(httpClientInstanceName);
HttpPost post = new HttpPost(url);
StringEntity str = new StringEntity(xml);
str.setContentType("text/xml");
post.setEntity(str);
response = httpClient.execute(post);
if (post != null){
post.abort();
}
if (httpClient !=null){
httpClient.close();
}
return response;
}
Then, in my AsyncTask of my fragment, I try to read the response using getEntity():
HttpResponse response = xmlUtil.sendXMLToURL("url", dataXML, "getList");
//Check if the request was sent successfully
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// Parse result to check success
responseText = EntityUtils.toString(response.getEntity());
if (!xmlParser.checkForSuccess(responseText, getActivity())){
//If webservice response is error
///TODO: Error management
return false;
}
}
And when I reach that line:
responseText = EntityUtils.toString(response.getEntity());
I get an exception: java.net.SocketException: Socket closed.
This behavior doesn't happen all the time, maybe every other time.
Just write
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(your url);
HttpResponse response = client.execute(post);
it should work.No need to write codes which makes confusion.
I also experienced the 'socket closed' exception when using a client instance built using HttpClientBuilder. In my case, I was calling HttpRequestBase.releaseConnection() on my request object within a finally block before processing the response object (in a parent method). Flipping things around solved the issue... (working code below)
try {
HttpResponse response = httpClient.execute(request);
String responseBody = EntityUtils.toString(response.getEntity());
// Do something interesting with responseBody
} catch (IOException e) {
// Ah nuts...
} finally {
// release any connection resources used by the method
request.releaseConnection();
}

Calling REST API with JSON response from setlist.fm

Anyone ever called the setlist.fm API from Android? I'm trying to execute the following code:
DefaultHttpClient client = new DefaultHttpClient();
HttpGet getRequest;
try
{
getRequest = new HttpGet(url);
HttpResponse getResponse = client.execute(getRequest);
HttpEntity getResponseEntity = getResponse.getEntity();
return getResponseEntity.getContent();
}
catch (Exception e)
{
Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
return null;
}
But I run into 2 issues it seems. The execute always seems to return correctly with a statusline of 200, but when I try to get the entity the length is -1 even though the response object shows a basichttpentity object was returned. Also, when I try to call getContent it blows up for some reason with a generic exception. A sample API call that I'm trying to use is http://api.setlist.fm/rest/0.1/search/setlists.json?artistName=Prince.
Any suggestions would be gladly welcomed.
Thanks!
I figured out the issue. It seems that the response coming back from the API is streamed and comes in chunks and I was attempting to grab the entity before it was finished. I removed this code:
HttpEntity getResponseEntity = getResponse.getEntity();
return getResponseEntity.getContent();
And replaced it with:
return EntityUtils.toString(getResponse.getEntity());

Parsing the XML Response in an Android Application

I want to Parse the XML Response in my Application using a SAX PArser, I don't know how to do that, So Can anybody please giude me to the right path.
An example with a little coding or a link will be OK.
Thanks,
david
try {
HttpClient client = new DefaultHttpClient();
String getURL = <URL>;
HttpGet get = new HttpGet(getURL);
HttpResponse responseGet = client.execute(get);
mResEntityGet = responseGet.getEntity();
if (mResEntityGet != null) {
//do something with the response
content = EntityUtils.toString(mResEntityGet);
}
} catch (Exception e) {
e.printStackTrace();
}
"content" will be the string of XML format, parse it using XML pull parser.

Categories

Resources