2015-08-20 90 views
0

這是我的網站: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; 
    } 
+0

您是否可能從PHP文件而不是預期的字符串文字返回「」500「」? – andrewdleach

+0

如果您訪問該網站,將出現500,同樣如果您查看頁面源,它​​將出現500 –

+0

嘗試'Integer.parseInt(out_string.replaceAll(「[\\ D]」,「」));'。我認爲你的HTTP庫傳遞了一些非十進制字符和輸出。 – iTurki

回答

1

正如在評論中討論:

我覺得你的HTTP lib中通過一些非十進制字符與輸出一起。試試這個:

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

[\\D]是一個正則表達式表示任何非十進制字符。

+0

謝謝。有效。 –

+0

Yupp ..很好的.. +1。還需要處理垃圾人物。 – user370305

1

有可能通過你的響應包含""內部的字符串,只需更換""

所以不喜歡它,

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 
} 

我建議你來處理異常也可以,如果你在你的Resposnse中有其他無效字符。

+0

仍然是同樣的錯誤。 –

+0

@Aakash - 比原因是什麼?在做downvote之前,請仔細閱讀問題,OP得到500 .. – user370305

0

對於你運行的Java版本檢查代碼。 java.lang.Integer.parse(Integer.java:375)

粗略地使用Firebug檢查該響應
我看到「我»¿500" ,這是肯定比‘500’我本來期望更多。

相關問題