Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用android emulator创建远程HTTP服务器的套接字_Android_Sockets_Emulation - Fatal编程技术网

使用android emulator创建远程HTTP服务器的套接字

使用android emulator创建远程HTTP服务器的套接字,android,sockets,emulation,Android,Sockets,Emulation,我对android编程非常陌生。我有一个程序,它使用套接字连接到远程主机,欺骗http请求,并解析结果。由于某些原因,我的套接字无法连接到主机。我已经在一个独立的环境中测试了这个程序,它连接良好,工作正常。我继续隔离了我需要的函数,并将其包含在我的活动类中,以这种方式调用它。我还尝试静态调用它,以及执行异步对象并从那里调用它。我的android模拟器上的互联网是通过浏览器工作的,我添加了 <uses-permission android:name="android.permission.

我对android编程非常陌生。我有一个程序,它使用套接字连接到远程主机,欺骗http请求,并解析结果。由于某些原因,我的套接字无法连接到主机。我已经在一个独立的环境中测试了这个程序,它连接良好,工作正常。我继续隔离了我需要的函数,并将其包含在我的活动类中,以这种方式调用它。我还尝试静态调用它,以及执行异步对象并从那里调用它。我的android模拟器上的互联网是通过浏览器工作的,我添加了

 <uses-permission android:name="android.permission.INTERNET"/>

到我的android清单。下面的代码显示了我现在的立场。我需要做什么

public class FlightNumResults extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    Intent intent = getIntent();
    String value = intent.getStringExtra("input"); //if it's a string you stored.
    //super.onCreate(savedInstanceState);
    System.out.println("HULLO");
    System.out.println(value);
    setContentView(R.layout.loading);

    Log.i("Excecution", "Complete");
    Hashtable<String, String> table = flightNumInfo(value);
    if (table == null)
        System.out.println("NULL");

    TableLayout results = (TableLayout) findViewById(R.id.resultTable);

    TableRow rowLabels = new TableRow(this);
    TableRow rowResults = new TableRow(this);
    rowLabels.setGravity(Gravity.CENTER);

    TableRow.LayoutParams params = new TableRow.LayoutParams();
    params.span = 4;

    //Column 1
    TextView destLabel = new TextView(this);  
    destLabel.setText("Destination");  
    destLabel.setTypeface(Typeface.SERIF, Typeface.BOLD);  
    TextView destResult = new TextView(this);  
    destResult.setText(table.get("destination"));  
    destResult.setGravity(Gravity.CENTER_HORIZONTAL);  

    rowLabels.addView(destLabel);  
    rowResults.addView(destResult);
    //Column 2
    TextView gateLabel = new TextView(this);  
    destLabel.setText("Gate");  
    destLabel.setTypeface(Typeface.SERIF, Typeface.BOLD);  
    TextView gateResult = new TextView(this);  
    destResult.setText(table.get("gate"));  
    destResult.setGravity(Gravity.CENTER_HORIZONTAL);  

    rowLabels.addView(gateLabel);  
    rowResults.addView(gateResult);
  //Column 3
    TextView scheduledLabel = new TextView(this);  
    destLabel.setText("Scheduled");  
    destLabel.setTypeface(Typeface.SERIF, Typeface.BOLD);  
    TextView scheduledResult = new TextView(this);  
    destResult.setText(table.get("scheduled"));  
    destResult.setGravity(Gravity.CENTER_HORIZONTAL);  

    rowLabels.addView(scheduledLabel);  
    rowResults.addView(scheduledResult);
  //Column 4
    TextView estimatedLabel = new TextView(this);  
    destLabel.setText("estimated");  
    destLabel.setTypeface(Typeface.SERIF, Typeface.BOLD);  
    TextView estimatedResult = new TextView(this);  
    destResult.setText(table.get("scheduled"));  
    destResult.setGravity(Gravity.CENTER_HORIZONTAL);  

    rowLabels.addView(estimatedLabel);  
    rowResults.addView(estimatedResult);

    results.addView(rowLabels);
    results.addView(rowResults);

    setContentView(R.layout.activity_flightnum_results);





}
public Hashtable<String, String> flightNumInfo(String flightNum) {

    Hashtable<String, String> table = new Hashtable<String, String>();
    System.out.println("Initializing socket");
    try {
        String request = "GET /flifo/servlet/DeltaFlifo?airline_code=DL&flight_number="
                + flightNum
                + "&flight_date="
                + DateCalc.getSlashDateAndTime()
                + "&request=main&DptText=ATL HTTP/1.1";

        Socket conn = new Socket(InetAddress.getByName("www.delta.com"), 80);
        System.out.println("initialized");
        PrintWriter wr = new PrintWriter(conn.getOutputStream());

        wr.println(request);
        wr.println("Host: www.delta.com");
        wr.println("Referer: http://www.delta.com/flifo/servlet/DeltaFlifo?airline_code=DL&request=main");
        wr.println("Connection: close\n");
        wr.flush();

        Scanner in = new Scanner(conn.getInputStream());
        String myResp = new String();
        String ans = new String();

        do {
            ans = in.nextLine();
            myResp += ans;
        } while (ans.indexOf("<span class=\"detailsLabel\"><br />") < 0);

        wr.close();
        in.close();
        // System.out.println(myResp);
        Pattern p = Pattern
                .compile("<td class=\"tableCell\" align=\"left\">&nbsp;\\s+Atlanta\\s+"
                        + "<a href=\"/content/www/en_US/traveling-with-us/airports-and-aircraft/airports/atlanta.html\">"
                        + "<!-- mp_trans_disable_start -->\\(ATL\\)<!-- mp_trans_disable_end --></a>\\s+<br />&nbsp;(.*?)"
                        + "<br />&nbsp; Gate <!-- mp_trans_disable_start -->([A-Z0-9]{2,3})<!-- mp_trans_disable_end -->\\s+"
                        + "</td>\\s+<td class=\"tableCell\">\\s+&nbsp;([A-Za-z0-9:]{6,9})<br />\\s+&nbsp;(.*?)</td>\\s+"
                        + "<td class=\"tableCell\">\\s+&nbsp;([a-z0-9*?:]{6,9}|[a-zA-Z\\s]{6,9})<br />\\s+&nbsp;(.*?)</td>\\s+" 
                        + "<td class=\"tableCell\">&nbsp;(.*?)\\(([A-Z]{3})\\)");


        Matcher m = p.matcher(myResp);
        if (m.find()) {
            table.put("number", flightNum);
            table.put("gate", m.group(2));
            table.put("scheduled", m.group(3));
            table.put("estimated", m.group(5));
            table.put("destination", m.group(8));

            return table;
        }
    } catch (Exception e) {
        return null;
    }
    return null;
}

}
公共类FlightNumResults扩展活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
Intent=getIntent();
String value=intent.getStringExtra(“输入”);//如果它是您存储的字符串。
//super.onCreate(savedInstanceState);
System.out.println(“HULLO”);
系统输出打印项次(值);
setContentView(R.layout.loading);
日志i(“执行”、“完成”);
哈希表=flightNumInfo(值);
如果(表==null)
System.out.println(“空”);
TableLayout结果=(TableLayout)findViewById(R.id.resultable);
TableRow rowLabels=新的TableRow(此);
TableRow rowResults=新的TableRow(此);
行标签。设置重力(重力。中心);
TableRow.LayoutParams params=新建TableRow.LayoutParams();
参数span=4;
//第1栏
TextView destLabel=新的TextView(此);
destLabel.setText(“目的地”);
destLabel.setTypeface(Typeface.SERIF,Typeface.BOLD);
TextView destResult=新的TextView(此);
destResult.setText(table.get(“destination”);
destResult.setGravity(重心水平);
rowLabels.addView(destLabel);
rowResults.addView(destResult);
//第2栏
TextView gateLabel=新的TextView(此);
destlab.setText(“门”);
destLabel.setTypeface(Typeface.SERIF,Typeface.BOLD);
TextView网关结果=新的TextView(此);
destResult.setText(table.get(“gate”);
destResult.setGravity(重心水平);
rowLabels.addView(gateLabel);
rowResults.addView(gateResult);
//第3栏
TextView scheduledLabel=新的TextView(此);
destlab.setText(“预定”);
destLabel.setTypeface(Typeface.SERIF,Typeface.BOLD);
TextView scheduledResult=新的TextView(此);
destResult.setText(table.get(“scheduled”);
destResult.setGravity(重心水平);
rowLabels.addView(scheduledLabel);
rowResults.addView(scheduledResult);
//第4栏
TextView estimatedLabel=新的TextView(此);
destLabel.setText(“估计”);
destLabel.setTypeface(Typeface.SERIF,Typeface.BOLD);
TextView estimatedResult=新的TextView(此);
destResult.setText(table.get(“scheduled”);
destResult.setGravity(重心水平);
rowLabels.addView(估计标签);
rowResults.addView(estimatedResult);
结果。添加视图(行标签);
results.addView(rowResults);
setContentView(R.layout.activity\u flightnum\u results);
}
公共哈希表flightNumInfo(字符串flightNum){
Hashtable=新的Hashtable();
System.out.println(“初始化套接字”);
试一试{
String request=“GET/flifo/servlet/DeltaFlifo?航空公司代码=DL和航班号=”
+flightNum
+“&航班日期=”
+DateCalc.getSlashDateAndTime()
+“&request=main&DptText=ATL-HTTP/1.1”;
插座连接=新插座(InetAddress.getByName(“www.delta.com”),80);
System.out.println(“已初始化”);
PrintWriter wr=新的PrintWriter(连接getOutputStream());
wr.println(请求);
wr.println(“主机:www.delta.com”);
wr.println(“参考:http://www.delta.com/flifo/servlet/DeltaFlifo?airline_code=DL&request=main");
wr.println(“连接:关闭\n”);
wr.flush();
扫描仪输入=新扫描仪(连接getInputStream());
字符串myResp=新字符串();
字符串ans=新字符串();
做{
ans=in.nextLine();
myResp+=ans;
}而(ans.indexOf(“
”)<0; wr.close(); in.close(); //系统输出打印项次(myResp); 模式p=模式 .compile(“\\s+Atlanta\\s+” +“\\s+
(.*)” +“
门([A-Z0-9]{2,3})\\s+” +“\\s+\\s+([A-Za-z0-9:{6,9})
\\s+(.*)\\s+” +“\\s+([a-z0-9*?:]{6,9}|[a-zA-Z\\s]{6,9})
\\s+(.*)\\s+” +“(.*?\\([A-Z]{3}\\)”; 匹配器m=p.Matcher(myResp); if(m.find()){ 表.put(“编号”,flightNum); 表1.put(“门”,m.group(2)); 表3.put(“预定”,m.group(3)); 表1.put(“估计”,m.group(5)); 表1.put(“目的地”,m.group(8)); 返回表; } }捕获(例外e){ 返回null; } 返回null; } }
下面是使用AsyncTask时我的代码的样子

public class FlightNumResults extends Activity {

public String flightNum;
public Hashtable<String, String> table;

@Override
protected void onCreate(Bundle savedInstanceState) {
    Intent intent = getIntent();
    flightNum = intent.getStringExtra("input"); // if it's a string you
                                                // stored.
    super.onCreate(savedInstanceState);
    System.out.println("HULLO");
    // System.out.println(value);

    setContentView(R.layout.loading);

    Log.i("Excecution", "Complete");
    FlightInfo fi = new FlightInfo();
    fi.execute();
    fi.flightNumInfo(flightNum);
    // Hashtable<String, String> table = fi.flightNumInfo(value);

    // System.out.println(table.get("destination"));

    // setContentView(R.layout.activity_flightnum_results);



}

public class FlightInfo extends AsyncTask<Void, Void, Void> {
    public Socket conn;
    public PrintWriter wr;
    public DataInputStream dis;
    public DataOutputStream dos;
    @Override
    protected Void doInBackground(Void... params) {
        try {
             conn = new Socket(InetAddress.getByName("www.delta.com"), 80);
        }catch (Exception e){
            Log.i("AsyncTank", "Can't create socket");
        }
        if (conn.isConnected()){
            try {
                dis = (DataInputStream)conn.getInputStream();
                dos = (DataOutputStream)conn.getOutputStream();
                Log.i("AsyncTank", "doInBackgoung: Socket created, Streams assigned");

            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.i("AsyncTank", "doInBackgoung: Cannot assign Streams, Socket not connected");
                e.printStackTrace();
            }
        } else {
            Log.i("AsyncTank", "doInBackgoung: Cannot assign Streams, Socket is closed");
        }
        return null;

        }
    protected void onPostExecute(Boolean result) {
        if (result) {
            Log.i("AsyncTask", "onPostExecute: Completed with an Error.");

        } else {
            Log.i("AsyncTask", "onPostExecute: Completed.");

        }
        setContentView(R.layout.activity_flightnum_results);

    }



        public  void flightNumInfo(String flightNum) {

              table = new Hashtable<String, String>();

                String request = "GET /flifo/servlet/DeltaFlifo?airline_code=DL&flight_number="
                        + flightNum
                        + "&flight_date="
                        + DateCalc.getSlashDateAndTime()
                        + "&request=main&DptText=ATL HTTP/1.1";

                //Socket conn = new Socket(InetAddress.getByName("www.delta.com"), 80);

                wr = new PrintWriter(dos);

                wr.println(request);
                wr.println("Host: www.delta.com");
                wr.println("Referer: http://www.delta.com/flifo/servlet/DeltaFlifo?airline_code=DL&request=main");
                wr.println("Connection: close\n");
                wr.flush();

                Scanner in = new Scanner(dis);
                String myResp = new String();
                String ans = new String();

                do {
                    ans = in.nextLine();
                    myResp += ans;
                } while (ans.indexOf("<span class=\"detailsLabel\"><br />") < 0);

                wr.close();
                in.close();

                // System.out.println(myResp);
                Pattern p = Pattern
                        .compile("<td class=\"tableCell\" align=\"left\">&nbsp;\\s+Atlanta\\s+"
                                + "<a href=\"/content/www/en_US/traveling-with-us/airports-and-aircraft/airports/atlanta.html\">"
                                + "<!-- mp_trans_disable_start -->\\(ATL\\)<!-- mp_trans_disable_end --></a>\\s+<br />&nbsp;(.*?)"
                                + "<br />&nbsp; Gate <!-- mp_trans_disable_start -->([A-Z0-9]{2,3})<!-- mp_trans_disable_end -->\\s+"
                                + "</td>\\s+<td class=\"tableCell\">\\s+&nbsp;([A-Za-z0-9:]{6,9})<br />\\s+&nbsp;(.*?)</td>\\s+"
                                + "<td class=\"tableCell\">\\s+&nbsp;([a-z0-9*?:]{6,9}|[a-zA-Z\\s]{6,9})<br />\\s+&nbsp;(.*?)</td>\\s+" 
                                + "<td class=\"tableCell\">&nbsp;(.*?)\\(([A-Z]{3})\\)");


                Matcher m = p.matcher(myResp);
                if (m.find()) {
                    table.put("number", flightNum);
                    table.put("gate", m.group(2));
                    table.put("scheduled", m.group(3));
                    table.put("estimated", m.group(5));
                    table.put("destination", m.group(8));


                }




        }
    }
}
公共类FlightNumResults扩展活动{
公共字符串flightNum;
公共哈希表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
Intent=getIntent();
flightNum=intent.getStringExtra(“输入”);//如果是您需要的字符串
//储存的。
super.onCreate(savedInstanceState);
System.out.println(“HULLO”);
//系统输出打印项次(值);
setContentView(R.layout.loading);
日志i(“执行”、“完成”);
FlightInfo fi=新的FlightInfo();
fi.execute();
fi.flightNumInfo(flightNum);
//哈希表=fi.flightNumInfo(值);
//System.out.println(table.get(“
conn = new Socket(InetAddress.getByName(new URL("http://www.delta.com").getHost()), 80);
dis = new DataInputStream(conn.getInputStream());
dos = new DataOutputStream(conn.getOutputStream());