A simple socket app in Android needs some special permissions to connect through the internet? -
a simple socket app in android needs special permissions connect through internet?
whats wrong code? exceptions when tryes connect input text ip, or 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 activity 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 socket outputstream out = s.getoutputstream(); printwriter output = new printwriter(out); // send http request 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 response boolean loop = true; stringbuilder sb = new stringbuilder(8096); while (loop) { if (in.ready()) { int = 0; while (i != -1) { = 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! host or ip unknown." ); } catch (ioexception e) { // todo auto-generated catch block text.settextcolor(0xffff0000); text.settext("unknown error. check internet connection!" ); } } else { text.settextcolor(0xffff0000); text.settext("error! please type host or ip" ); } } }
do have
<uses-permission android:name="android.permission.internet" />
in manifest? outside application tags.
Comments
Post a Comment