2013-05-03 114 views
1

運行應用程序時,操作超時此代碼的工作在模擬器罰款,但在真機上它給java.net.SocketException異常:真實設備

java.net.SocketException異常:操作超時 我得到了一個運行在我的xampp服務器上的php腳本。

package com.example.new1; 


import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.widget.TextView; 

    public class MainActivity extends Activity { 
    TextView tx; 
    StringBuilder stringBuilder; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    tx= (TextView)findViewById(R.id.text); 
} 

public void func(View view) 
{ 
    //tx.setText("Working fine till here."); 
    new FetchSQL().execute(); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 
private class FetchSQL extends AsyncTask<String,Void,String> 

{ 

    @Override 
    protected String doInBackground(String... arg0) { 
      URL url = null; 
      BufferedReader reader = null; 

      String myUrl = "http://10.22.35.4:80/conc2.php"; 
     try 
     { url =new URL(myUrl); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      connection.setRequestMethod("GET"); 
      connection.setReadTimeout(15*10000); 
      connection.connect(); 
      reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
      stringBuilder = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) 
      { 
      stringBuilder.append(line + "\n"); 

      } 
     // TODO Auto-generated method stub 
     return stringBuilder.toString(); 
     } 
     catch(final Exception e) 
     { 
      return e.toString(); 
      } 

    } 

    protected void onPostExecute(final String result) 
    { 
     tx.setText(result); 
    } 


} 

}

當我點擊鏈接它需要時間IVE集的AMT在我的代碼,然後失敗給了我在我的TextView錯誤。請幫忙

我的php代碼。

<?php 
// attempt a connection 
$dbh = pg_connect("host=10.22.35.11 dbname=iwmp_dev2 user=postgres "); 

if (!$dbh) { 
die("Error in connection: " . pg_last_error()); 
}  

// execute query 
//$sql = $_POST['pLat']; 
$sql = "SELECT officer_name FROM iwmp_officer"; 

$result = pg_query($dbh, $sql); 
if (!$result) { 
die("Error in SQL query: " . pg_last_error()); 
}  
$array = array(); 
// iterate over result set 
// print each row 
while ($row = pg_fetch_assoc($result, null)) { 
    $i++; 
    $array = implode('+',$row); 
echo $array; 

} 


    // free memory 
    pg_free_result($result);  

    // close connection 
    pg_close($dbh); 
     ?>  
+0

你把你的php文件放在哪裏? – Nitish 2013-05-03 07:19:51

+0

爲你的http連接設置一個http連接超時http://stackoverflow.com/questions/3075506/http-connection-timeout-on-android-not-working – Raghunandan 2013-05-03 07:26:12

+0

請參考這篇文章,這可能會有所幫助http: //stackoverflow.com/q/8240338/1292544 – Nitish 2013-05-03 07:33:12

回答

1

java.net.SocketException當您嘗試訪問的端口未關閉或不可用時,會出現異常。它需要你指定的時間來搜索端口然後出去。

首先,嘗試在移動網頁瀏覽器上調用此服務以檢查它是否可用。如果沒有顯示,表示您的設備沒有連接到該文件所在的網絡。

您的防火牆可能不允許ping您的端口。當您使用仿真器時,它可以在同一臺PC上運行,但是如果是真實的設備,它將通過本地網絡連接,並且有時候防火牆不允許。解決方案:在防火牆上取消阻止此請求,或通過關閉防火牆嘗試此操作。