0

I don't know why i get a null value when i call the GetPHPData() function. The "out" variable returns nothing (""). I make a Toast.makeTest and it returns empty string. Please help. This is my code:

public class PHPConnect extends  Activity
{
    String url = "http://122.2.8.226/MITBookstore/sqlconnect.php";
    HttpURLConnection urlConnection = null;
    String out = null;
    public String GetPHPData()
    {
        try {

            urlConnection = (HttpURLConnection) new URL(url).openConnection();
            urlConnection.setConnectTimeout(5000);
            urlConnection.setReadTimeout(10000);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try
        {
            BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            out = readStream(in);
        }
        catch (Exception e)
        {
            Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
        }
        finally
        {
            urlConnection.disconnect();
            return out;
        }

    }
    private String readStream(BufferedReader is)
    {
        try
        {
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            int i = is.read();
            while(i != -1)
            {
                bo.write(i);
                i = is.read();
            }
            return bo.toString();
        } catch (IOException e)
        {
            return e.getMessage();
        }
    }
}

By the way, im running a wamp server and I port forwarded my router, on local host, the url works, but on remote connection, it won't return a string. You can try out the url, the result is: "This is the output:emil"

7
  • Are you getting any exception? Commented May 18, 2015 at 7:01
  • You don't seem to be calling connect() on the URLConnection instance. Commented May 18, 2015 at 7:03
  • i dont get any exception @Android007 Commented May 18, 2015 at 7:04
  • @CPUTerminator i put urlConnection.connect(); after urlConnection.setReadTimeout(10000); and my emulator gets an error. Unfortunately, "My App" has stopped working. Commented May 18, 2015 at 7:12
  • You now have a stack trace, update the code displayed here and paste the stack trace. Commented May 18, 2015 at 7:14

1 Answer 1

1

Can you please try below piece of code which is working for me, also add INTERNET permission in android manifest file. Still if it is not working then may be issue with server end then try to debug it.

URL url;
    try {
        url = new URL("myurl");

        HttpURLConnection urlConnection = (HttpURLConnection) url
                .openConnection();

        InputStream in = urlConnection.getInputStream();

        InputStreamReader isw = new InputStreamReader(in);

        int data = isw.read();
        while (data != -1) {
            char current = (char) data;
            data = isw.read();
            System.out.print(current);
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
Sign up to request clarification or add additional context in comments.

4 Comments

i have a question, im debugging this with android emulator. what is the final output string for that code? is it "current"?
current is string variable which will hold and print your response of web service. 122.2.8.226/MITBookstore/sqlconnect.php
current is string variable. No it is a char variable. You read and print the received bytes one by one. That does not make sense. Read them in one String instead as OP has asked. Using readLine() and so on.
Sorry my bad, its char variable. :D You can do by any way but that should not be blank. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.