這是我的網站:http://daniandroid.honor.es/getAllCustomers.php 當您訪問該網站時,您會得到一個簡單的文本「500」。Java不會Integer.parseInt(字符串)
好的。
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= "500";
一切更換out_string好。
我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「」? – andrewdleach
如果您訪問該網站,將出現500,同樣如果您查看頁面源,它將出現500 –
嘗試'Integer.parseInt(out_string.replaceAll(「[\\ D]」,「」));'。我認爲你的HTTP庫傳遞了一些非十進制字符和輸出。 – iTurki