java.net.socketException:在真实设备上运行app时的操作超时

时间:2013-05-03 07:12:14

标签: android connection out 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);
    }


}

}

当我点击按钮时,我需要在我的代码中设置时间,然后在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);
       ?>       

1 个答案:

答案 0 :(得分:1)

当您尝试访问的端口未关闭或不可用时,会出现

java.net.SocketException异常。它需要您指定用于搜索端口然后离开的时间量。

首先,尝试在您的移动网络浏览器上调用此服务,以检查它是否可用。如果未显示,则表示您的设备未连接到此文件所在的网络。

您的防火墙可能无法ping您的端口。当您使用模拟器时,它可以在同一台PC上运行,但是如果是真实设备,则它通过本地网络连接,而防火墙则不允许。解决方案:在防火墙上取消阻止此请求,或通过关闭防火墙来尝试此操作。