When obtaining the information of server return, LOGCAT display 1 2 3. Result is 1, 5, 6.
When judge if statement, it has the error. I think the result is 1, but it still wrong.
try {
URL url=new URL(urlstr);
URLConnection conn = null;
Log.i("Text", "1");
try {
conn = url.openConnection();
Log.i("Text", "2");
InputStream in=conn.getInputStream();
Log.i("Text", "3");
byte[] buffer=new byte[in.available()];
in.read(buffer);
String result=new String(buffer);
Log.i("Text", "result="+result);
if(result.equals("1"))
{
Log.i("Text", "4");
} else {
Log.i("Text", "5");
}
Log.i("Text", "6");
}
}
I don’t know why. Could anyone help me? Thanks very much.
It’s unclear exactly what you’re trying to achieve, but these three lines have three problems:
available(). Instead, create a fixed size buffer (it sounds like you’re not expecting much data, so just a small buffer would be fine.) What if there’s no data available when you callavailable()? You’ll be creating an empty array – surely you don’t want to do that.InputStream.read(). Never do that. You have no idea how much data has actually been read.Stringfrom abyte[]without specifying the encoding. Don’t do that. Which encoding is the data actually using?My guess is that you’re either passing an empty array in, or you’re actually reading more than one byte.