Android中的简单套接字应用需要一些特殊权限才能通过互联网连接?

Android中的简单套接字应用需要一些特殊权限才能通过互联网连接?,第1张

概述Android中的简单套接字应用需要一些特殊权限才能通过互联网连接?我的代码有什么问题?当它尝试连接到输入文本IP或HOST时,它总是会获得异常.packageWhat.HTTPServer;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava

Android中的简单套接字应用需要一些特殊权限才能通过互联网连接?

我的代码有什么问题?当它尝试连接到输入文本IP或HOST时,它总是会获得异常.

package What.httpServer;import java.io.BufferedReader;import java.io.IOException;import java.io.inputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import java.net.socket;import java.net.UnkNownHostException;import androID.app.Activity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.TextVIEw;public class WhathttpServerActivity extends Activity implements OnClickListener {    /** Called when the activity is first created. */    @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.main);        button button = (button)findVIEwByID(R.ID.button1);        button.setonClickListener(this);    }    public voID onClick(VIEw v) {             // Todo auto-generated method stub            TextVIEw text = (TextVIEw)findVIEwByID(R.ID.textVIEw4);            EditText textFIEld = (EditText) findVIEwByID(R.ID.editText1);             if (textFIEld.getText().toString().length() > 3)            {                String host = textFIEld.getText().toString();                String retorno = "";                text.setTextcolor(0xff0000ff);                text.setText("Connecting...");                try {                    Socket s = new Socket(host, 80);                                       //outgoing stream redirect to socket                    OutputStream out = s.getoutputStream();                    PrintWriter output = new PrintWriter(out);                      // send an http request to the web server                      output.println("GET / http/1.1");                      output.println("Host: " + host + ":80");                      output.println("Connection: Close");                      output.println();                    BufferedReader in = new BufferedReader(new inputStreamReader(s.getinputStream()));                        // read the response                          boolean loop = true;                          StringBuilder sb = new StringBuilder(8096);                          while (loop) {                            if (in.ready()) {                              int i = 0;                              while (i != -1) {                                i = in.read();                                sb.append((char) i);                              }                              loop = false;                            }                          }                          retorno = sb.toString();                    //Close connection                    s.close();                    text.setTextcolor(0xff0000ff);                    text.setText("Your server runs: \n"                             + retorno );                                    } catch (UnkNownHostException e) {                    // Todo auto-generated catch block                    text.setTextcolor(0xffff0000);                    text.setText("Error! The Host or IP is unkNown." );                } catch (IOException e) {                    // Todo auto-generated catch block                    text.setTextcolor(0xffff0000);                    text.setText("UnkNown error. Check your internet connection!" );                }                           } else {                text.setTextcolor(0xffff0000);                text.setText("Error! Please type your host or IP" );            }    }     }

解决方法:

你有没有

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

在你的清单?应用程序标签之外.

总结

以上是内存溢出为你收集整理的Android中的简单套接字应用需要一些特殊权限才能通过互联网连接?全部内容,希望文章能够帮你解决Android中的简单套接字应用需要一些特殊权限才能通过互联网连接?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1105224.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存