运行在实际设备应用程序时,操作超时:产生java.net.SocketException [英] java.net.socketException:operation time out when running app on real device

查看:108
本文介绍了运行在实际设备应用程序时,操作超时:产生java.net.SocketException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这code正常工作在模拟器,但在真实的设备它给

  

产生java.net.SocketException 的:操作超时   香港专业教育学院有一个PHP脚本我的XAMPP服务器上运行。

 包com.example.new1;


 进口java.io.BufferedReader中;
 进口java.io.InputStreamReader中;
 进口java.net.HttpURLConnection中;
 进口的java.net.URL;

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

  公共类MainActivity延伸活动{
  TextView的TX;
  StringBuilder的StringBuilder的;
@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    TX =(TextView中)findViewById(R.id.text);
}

公共无效FUNC(查看视图)
{
    //tx.setText("Working细到这里)。
     新FetchSQL()执行()。
}

@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    //充气菜单;这增加了项目操作栏,如果它是present。
    。getMenuInflater()膨胀(R.menu.activity_main,菜单);
    返回true;
}
私有类FetchSQL扩展的AsyncTask<字符串,太虚,字符串>

{

    @覆盖
    保护字符串doInBackground(字符串...为arg0){
            网址URL = NULL;
            BufferedReader中读= NULL;

            字符串myUrl =htt​​p://10.22.35.4:80/conc2.php;
        尝试
        {URL =新的URL(myUrl);
          HttpURLConnection的连接=(HttpURLConnection类)url.openConnection();
          connection.setRequestMethod(GET);
          connection.setReadTimeout(15 * 10000);
          connection.connect();
          读者=新的BufferedReader(新的InputStreamReader(connection.getInputStream()));
          StringBuilder的=新的StringBuilder();
          串线= NULL;
          而((行= reader.readLine())!= NULL)
          {
            stringBuilder.append(行+\ N);

          }
        // TODO自动生成方法存根
        返回stringBuilder.toString();
        }
        赶上(最终例外五)
        {
             返回e.toString();
          }

    }

    保护无效onPostExecute(最后弦乐结果)
    {
        tx.setText(结果);
    }


}
 

}

当我点击按钮,它需要时间的香港专业教育学院设置的AMT在我的code,然后失败给了我在我的TextView的错误。请大家帮忙

我的PHP code。

 < PHP
//尝试连接
 $胸径=函数pg_connect(主机= 10.22.35.11 DBNAME = iwmp_dev2用户= Postgres的);

 如果(!$胸径){
 死亡(在连接错误:pg_last_error());
 }

 //执行查询
 // $ SQL = $ _ POST ['高原'];
 $ SQL =选择officer_name从iwmp_officer;

 $结果= pg_query($胸径,$ SQL);
 如果(!$结果){
 死亡(在SQL查询时出现错误:pg_last_error());
 }
 $阵列=阵列();
//遍历结果集
//打印每行
而($行= pg_fetch_assoc($结果,NULL)){
  $ I ++;
  $阵列=破灭('+',$行);
 回声$阵列;

 }


    //空闲内存
    pg_free_result($结果);

     //关闭连接
     pg_close($胸径);
       ?>
 

解决方案

当您尝试访问的端口未关闭或不可用java.net.SocketException异常异常来。它利用了已用于搜索的端口指定的时间量,然后失控。

首先,尝试调用该服务的移动网络浏览器来检查它是否可用。如果它没有显示装置的设备没有连接到这个文件所在的网络

这可能是您的防火墙不允许ping你的端口。当你跟你的模拟器它的工作原理,因为它在同一台PC上,但如果你真正的设备是通过本地网络和一定的时间防火墙不允许连接。解决方案:取消阻止此请求的防火墙或关闭您的防火墙试试这个

this code works fine on emulator, but on real device it gives

java.net.SocketException:the operation timed out ive got a php script running on my xampp server.

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);
    }


}

}

when i click on button it takes the amt of time ive set in my code, and then fails giving me the error in my textview. please help

my php code.

<?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);
       ?>       

解决方案

java.net.SocketException exception comes when the port that you are trying to reach is not closed or not available. It takes the amount of time you have specified for searching the port and then gets out.

Firstly, try to call this service on your mobile web browser to check whether it is available or not. If it does not show that means your device is not connected to the network on which this file resides.

It may be possible that your firewall is not allowing to ping your port. When you are doing with your emulator it works since its on same PC but in case of your real device it connects via local network and some time Firewall does not allow. Solution: unblock this request on firewall or try this by closing your firewall.

这篇关于运行在实际设备应用程序时,操作超时:产生java.net.SocketException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆