java.net.socketException:operation time out when running app on real device(java.net.socketException:在真实设备上运行应用程序时操作超时)
问题描述
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 + "
");
}
// 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:在真实设备上运行应用程序时操作超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:java.net.socketException:在真实设备上运行应用程序时操作超时
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01