0

This is my site: http://daniandroid.honor.es/getAllCustomers.php when you visit the site, you get a simple text "500".

OK.

 final ourHTTP hi = new ourHTTP();

        out_string = hi.getWebPage("http://daniandroid.honor.es/getAllCustomers.php");

getWebPage returns the string(content).

 int veriff = Integer.parseInt(out_string);

         if(veriff>1)
         {
             final_form.setText("ya");
         }

final_form is a TextView on my xml file (activity_second.xml)

Application will crash. ERROR:

at com.android.interval.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)

Caused by: java.lang.NumberFormatException: Invalid int: "500"
at java.lang.Integer.parse(Integer.java:375)

If I replace the out_string with this

out_string= "500";

everything is good.

My getAllCustomers.php files contains (source):

<?php
echo"500";
?>

Here is also the method to get the content.

public String getWebPage(String adress)
{
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet();

    InputStream inputStream = null;
    String response = null;


    try{
        URI uri = new URI(adress);
        httpGet.setURI(uri);

        HttpResponse httpResponse = httpClient.execute(httpGet);


        inputStream = httpResponse.getEntity().getContent();
        Reader reader = new InputStreamReader(inputStream, "UTF-8");
        int inChar;
        StringBuffer stringBuffer = new StringBuffer();

        while((inChar = reader.read()) != -1){

            stringBuffer.append((char)inChar);
        }

        response = stringBuffer.toString();

    }catch(ClientProtocolException e)
    {
        Log.e(adress, "error");
    }catch(IOException e)
    {
        Log.e(response, "error");
        //
    }catch(URISyntaxException e)
    {
        Log.e(response, "error"); 
    }

    return response;
    }
5
  • Are you perhaps getting ""500"" returned from the PHP file instead of an expected String literal? Commented Aug 20, 2015 at 20:14
  • if you visit the site it will appear 500, also if you view source of page it will appear 500 Commented Aug 20, 2015 at 20:17
  • Try Integer.parseInt(out_string.replaceAll("[\\D]",""));. I think your HTTP lib passes some non-decimal chars along with the output. Commented Aug 20, 2015 at 20:19
  • life saver. IT WORKED. THANKS Commented Aug 20, 2015 at 20:28
  • @DaniSteptu You are Welcome. I will add it as an answer. Commented Aug 20, 2015 at 20:31

3 Answers 3

1

As discussed in the comments:

I think your HTTP lib passes some non-decimal chars along with the output. Try this:

int veriff = Integer.parseInt(out_string.replaceAll("[\\D]",""));

Where [\\D] is a regex denote any non-decimal character.

Sign up to request clarification or add additional context in comments.

1 Comment

Yupp.. Nice catch.. +1. Need to handle junk characters also.
1

May be possible your response contains "" internally in String, Just replace ""

So do it like,

out_string = hi.getWebPage("http://daniandroid.honor.es/getAllCustomers.php");

if(out_string.contains("\"")) // change according to your response, if it contains other charcter
{
 out_string = out_string.replace("\"", "")
}

try
{
 int veriff = Integer.parseInt(out_string);

 if(veriff>1)
 {
  final_form.setText("ya");
 } 
}catch(NumberFormatException ex)
{
   // Handle exception
}

I would suggest you to handle Exception also, May be if you have other invalid character in your resposnse..

1 Comment

@Aakash - Than what is the reason? Before do downvote just read question carefully, OP get response with 500..
0

For the version of Java you are running against examine the code. java.lang.Integer.parse(Integer.java:375)

Crudely using Firebug and examining the response
I see "500" which is certainly more than the "500" I would have expected.

Comments

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.