K
K
Kurban Mammaev2015-04-01 20:46:04
PHP
Kurban Mammaev, 2015-04-01 20:46:04

How to read data from php script in android application?

Hello. I have an android app. I asked a question about it, they advised me to write a script that the application will access and update the change data. The script was written and posted on the hosting:

<?php
$gos = "2500";
$baz = "4050";

echo $gos.'<br />';
echo $baz;

?>

The script just outputs 2 numeric values. So, the task is for the android application to read them and save them. How can you read variables from a script using Java? Please give a detailed answer, because. in php and server applications i am zero.
-------------------------------------------------- -------------------------------------------------- ----------------------
Thanks for the replies.
Here's what I found:
public static void connect(String url)
    {

        HttpClient httpclient = new DefaultHttpClient();

        HttpGet httpget = new HttpGet(url);

        HttpResponse response;
        try {
            response = httpclient.execute(httpget);
            Log.i("Praeda", response.getStatusLine().toString());

            HttpEntity entity = response.getEntity();

            if (entity != null) 
            {
                InputStream instream = entity.getContent();
                String result = convertStreamToString(instream);

                instream.close();
            }


        } catch (Exception e) {}
    }

    private 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) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

As I understand it, the code saves the page, then the content is processed in the second function, where it is converted to a string and assigned to the result variable. The question is how to assign the value of result to a global variable? I think I'm wrong somewhere. the variable is empty for some reason.
Php script above, maybe something is wrong in it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2015-04-01
@Axe98

I did based on an example like this
stackoverflow.com/questions/13458878/sending-post-...

I
IceJOKER, 2015-04-01
@IceJOKER

Have you tried searching?
There is no magic there, a simple download of the page, and there is a lot of information in the search about this and even your question.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question