【发布时间】:2015-08-20 20:08:59
【问题描述】:
这是我的网站:http://daniandroid.honor.es/getAllCustomers.php 当您访问该网站时,您会收到一个简单的文本“500”。
好的。
final ourHTTP hi = new ourHTTP();
out_string = hi.getWebPage("http://daniandroid.honor.es/getAllCustomers.php");
getWebPage 返回字符串(内容)。
int veriff = Integer.parseInt(out_string);
if(veriff>1)
{
final_form.setText("ya");
}
final_form 是我的 xml 文件 (activity_second.xml) 上的 TextView
应用程序将崩溃。 错误:
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)
如果我用这个替换 out_string
out_string= "500";
一切都很好。
我的 getAllCustomers.php 文件包含(来源):
<?php
echo"500";
?>
这里也是获取内容的方法。
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;
}
【问题讨论】:
-
您是否可能从 PHP 文件中返回 ""500"" 而不是预期的字符串文字?
-
如果您访问该站点,它将显示500,如果您查看页面源,它将显示500
-
试试
Integer.parseInt(out_string.replaceAll("[\\D]",""));。我认为您的 HTTP 库将一些非十进制字符与输出一起传递。 -
救命稻草。有效。谢谢
-
@DaniSteptu 欢迎您。我将其添加为答案。