Show table content from PHP to android table - android

i have this high score board in http://drymvizion.atwebpages.com/ and i want a simple way to click a button and show this content in my android application without opening browser. I am a bit new in android developing and i hope it's easy. Thanks ;)

You could use a HttpGet request to get the contents, or better use a xml parser, such as JSoup. For example:
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("site.name");
try
{
get.addHeader("Accept-Charset","utf-8");
HttpResponse response = client.execute(get);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String result = "";
while ((result = rd.readLine()) != null)
{
Log.e("HttpResponse", result);
if (result.length() > 0)
{
// do something with the source
}
}
}
catch (IOException e)
{
e.printStackTrace();
}

Have a look at WebView's
http://developer.android.com/reference/android/webkit/WebView.html

Related

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.

Require Help while Reading huge data (7MB or more) from Httpclient in Android

Require your Help for fetching the json response from the httpclient in Android since the following code mentioned is making the Application to crash on android Devices particularly on the GingerBread device , since the JSON Response is very huge in size (may be 7 MB) .
So I wanted to know any alternative way for reading the JSONresponse from the Httpclient, since the present implementation is consuming too much of memory and making my application to crash on lower end devices.
It would be very greatful for any suggestions or help for solving this problem .
HttpClient httpClient = new DefaultHttpClient(ccm, params);
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("Cache-Control", "no-cache; no-store");
HttpResponse httpResponse = httpClient.execute(httpGet);
response = Utils.convertStreamToString(httpResponse.getEntity().getContent());
public static String convertStreamToString(InputStream is)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
//System.gc();
sb.append(line).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
You can use Google Volley for you networking. Among a lot of other things, it has a built in method to retrieve JSON objects, regardless of size.
Give it a try.
You could try this:
public static String convertStreamToString(InputStream is)
{
try
{
final char[] buffer = new char[0x10000];
StringBuilder out = new StringBuilder();
Reader in = new InputStreamReader(is, "UTF-8");
int read;
do
{
read = in.read(buffer, 0, buffer.length);
if (read > 0)
{
out.append(buffer, 0, read);
}
} while (read >= 0);
in.close();
return out.toString();
} catch (IOException ioe)
{
throw new IllegalStateException("Error while reading response body", ioe);
}
}
Google Android ships with an extremely outdated fork of Apache HttpClient. However, the base principles still apply. The most efficient way of processing HTTP responses with Apache HttpClient is by using a ResponseHandler. See my answer to a similar question for details

How do I use the command: HttpEntity?

I wish to settle my long term problem by this question and hope you guys would help, but firstly; I have been having issues to connect to a HTTPS self-signed certificate server for almost 3 weeks. Despite the multiple solutions here, I cannot seem to resolve my problem. Probably I did not know how to use it properly or did not have some files or imported the correct libraries.
I came across some websites that requires me to download a certificate from the https site that I am trying to connect into, and when I did that. I have to do the some steps before I can use the certificate or keystore that I created. I got this solution from this website:
Android: Trusting SSL certificates
// Instantiate the custom HttpClient
DefaultHttpClient client = new MyHttpClient(getApplicationContext());
HttpGet get = new HttpGet("https://www.mydomain.ch/rest/contacts/23");
// Execute the GET call and obtain the response
HttpResponse getResponse = client.execute(get);
HttpEntity responseEntity = getResponse.getEntity();
I have a problem, after the last line, as stated above. What do I do with the responseEntity? How do I use it if I wish to display the https website on a WebView? Some help and explanation would be nice :)
If you want the content from the HttpEntity the correct way does not include calling HttpEntity#getContent() to retrieve a stream and doing tons of pointless stuff already available in the Android SDK.
Try this instead.
// Execute the GET call and obtain the response
HttpResponse getResponse = client.execute(get);
HttpEntity responseEntity = getResponse.getEntity();
// Retrieve a String from the response entity
String content = EntityUtils.toString(responseEntity);
// Now content will contain whatever the server responded with and you
// can pass it to your WebView using #loadDataWithBaseURL
Consider using WebView#loadDataWithBaseURL when displaying content - it behaves a lot nicer.
You need to call responseEntity.getContent() to get response in InputStream against your requested URL. Use that stream in your way to present data as you want. For example, if the expected data is String, so you may simply convert this stream into string with the following method:
/**
* Converts InputStream to String and closes the stream afterwards
* #param is Stream which needs to be converted to string
* #return String value out form stream or NULL if stream is null or invalid.
* Finally the stream is closed too.
*/
public static String streamToString(InputStream is) {
try {
StringBuilder sb = new StringBuilder();
BufferedReader tmp = new BufferedReader(new InputStreamReader(is),65728);
String line = null;
while ((line = tmp.readLine()) != null) {
sb.append(line);
}
//close stream
is.close();
return sb.toString();
}
catch (IOException e) { e.printStackTrace(); }
catch (Exception e) { e.printStackTrace(); }
return null;
}
InputStream is = responseEntity.getContent();
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
String result=sb.toString();
is.close();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
you will have all the content in the String "result"

how to load a big webpage into a string

I'm a novice with Java and Android, but not to programming and HTTP. This HTTP GET method, mostly copied from other examples using the Apache HTTP classes, only retrieves the first few K of a large webpage. I checked that the webpage does not have lines longer than 8192 bytes (is that possible?), but out of webpages around 40K I get back maybe 6K, maybe 20K. The number of bytes read does not seem to have a simple realtionship with the total webpage size, or the webpage modulus 8192, or with the webpage content.
Any ideas folks?
Thanks!
public static String myHttpGet(String url) throws Exception {
BufferedReader in = null;
try {
HttpClient client = getHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sbuffer = new StringBuffer("");
String line = "";
while ((line = in.readLine()) != null) {
sbuffer.append(line + "\n");
}
in.close();
String result = sbuffer.toString();
return result;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
No need to write you own HttpEntity-to-String code, try EntityUtils instead:
// this uses the charset the server encoded the entity in
String result = EntityUtils.toString(entity);
It looks as if the problem is with pages from a certain website starting Goo... I'm not having this problem with large pages from other sites. So the code is probably OK.

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