android - IOException: socket failed: ACCESS (Permission denied) -
i'm trying make simple android client-server app send string server.
client:
public class mainactivity extends actionbaractivity { edittext et; button btn; private socket client; private printwriter printwriter; private string s; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); et = (edittext) findviewbyid(r.id.et); btn = (button) findviewbyid(r.id.btn); btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { s = et.gettext().tostring(); et.settext(""); try { client = new socket("192.168.1.14", 4444); printwriter = new printwriter(client.getoutputstream(), true); printwriter.write(s); printwriter.flush(); printwriter.close(); client.close(); } catch (unknownhostexception ex) { toast.maketext(getapplicationcontext(), "unknownhostexception: " + ex.getmessage(), toast.length_long).show(); } catch (ioexception ex) { toast.maketext(getapplicationcontext(), "ioexception: " + ex.getmessage(), toast.length_long).show(); } } }); } }
server:
public class main { private static serversocket serversocket; private static socket socket; private static inputstreamreader inreader; private static bufferedreader bufferedreader; private static string s; public static void main(string[] args) { char port = 4444; try { serversocket = new serversocket(port); } catch (ioexception e) { system.out.println("could not listen on port " + port); } system.out.println("server started. listening on port" + port); while (true) { try { socket = serversocket.accept(); inreader = new inputstreamreader(socket.getinputstream()); bufferedreader = new bufferedreader(inreader); s = bufferedreader.readline(); system.out.println(s); inreader.close(); socket.close(); } catch (ioexception e) { system.out.println("problen in message reading!"); } } } }
androidmanifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test.eyalcherevatzki.clientserver" > <uses-feature android:name="android.hardware.type.watch" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.access_wifi_state" /> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@android:style/theme.devicedefault" > <activity android:name=".mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
logcat:
04-25 12:27:30.386 18000-18000/com.test.eyalcherevatzki.clientserver v/zygote﹕ switching descriptor 45 /dev/null 04-25 12:27:30.386 18000-18000/com.test.eyalcherevatzki.clientserver v/zygote﹕ switching descriptor 9 /dev/null 04-25 12:27:30.386 18000-18000/com.test.eyalcherevatzki.clientserver d/dalvikvm﹕ late-enabling checkjni 04-25 12:27:30.436 18000-18000/com.test.eyalcherevatzki.clientserver d/activitythread﹕ handlebindapplication:com.test.eyalcherevatzki.clientserver 04-25 12:27:30.436 18000-18000/com.test.eyalcherevatzki.clientserver d/activitythread﹕ settargetheaputilization:0.75 04-25 12:27:30.436 18000-18000/com.test.eyalcherevatzki.clientserver d/activitythread﹕ settargetheapminfree:2097152 04-25 12:27:30.476 18000-18000/com.test.eyalcherevatzki.clientserver i/dalvikvm﹕ dexopt: illegal method access (call landroid/content/res/typedarray;.<init> (landroid/content/res/resources;[i[ii)v landroid/content/res/xresources$xtypedarray;) 04-25 12:27:30.476 18000-18000/com.test.eyalcherevatzki.clientserver i/dalvikvm﹕ not find method android.content.res.typedarray.<init>, referenced method android.content.res.xresources$xtypedarray.<init> 04-25 12:27:30.476 18000-18000/com.test.eyalcherevatzki.clientserver w/dalvikvm﹕ vfy: unable resolve direct method 82: landroid/content/res/typedarray;.<init> (landroid/content/res/resources;[i[ii)v 04-25 12:27:30.476 18000-18000/com.test.eyalcherevatzki.clientserver d/dalvikvm﹕ vfy: replacing opcode 0x70 @ 0x0002 04-25 12:27:30.506 18000-18000/com.test.eyalcherevatzki.clientserver i/dalvikvm﹕ not find method android.content.res.typedarray.getchangingconfigurations, referenced method android.support.v7.internal.widget.tinttypedarray.getchangingconfigurations 04-25 12:27:30.506 18000-18000/com.test.eyalcherevatzki.clientserver w/dalvikvm﹕ vfy: unable resolve virtual method 584: landroid/content/res/typedarray;.getchangingconfigurations ()i 04-25 12:27:30.506 18000-18000/com.test.eyalcherevatzki.clientserver d/dalvikvm﹕ vfy: replacing opcode 0x6e @ 0x0002 04-25 12:27:30.506 18000-18000/com.test.eyalcherevatzki.clientserver i/dalvikvm﹕ not find method android.content.res.typedarray.gettype, referenced method android.support.v7.internal.widget.tinttypedarray.gettype 04-25 12:27:30.506 18000-18000/com.test.eyalcherevatzki.clientserver w/dalvikvm﹕ vfy: unable resolve virtual method 606: landroid/content/res/typedarray;.gettype (i)i 04-25 12:27:30.506 18000-18000/com.test.eyalcherevatzki.clientserver d/dalvikvm﹕ vfy: replacing opcode 0x6e @ 0x0002 04-25 12:27:30.546 18000-18000/com.test.eyalcherevatzki.clientserver i/dalvikvm﹕ not find method android.content.res.resources.getdrawable, referenced method android.support.v7.internal.widget.resourceswrapper.getdrawable 04-25 12:27:30.546 18000-18000/com.test.eyalcherevatzki.clientserver w/dalvikvm﹕ vfy: unable resolve virtual method 547: landroid/content/res/resources;.getdrawable (ilandroid/content/res/resources$theme;)landroid/graphics/drawable/drawable; 04-25 12:27:30.546 18000-18000/com.test.eyalcherevatzki.clientserver d/dalvikvm﹕ vfy: replacing opcode 0x6e @ 0x0002 04-25 12:27:30.546 18000-18000/com.test.eyalcherevatzki.clientserver i/dalvikvm﹕ not find method android.content.res.resources.getdrawablefordensity, referenced method android.support.v7.internal.widget.resourceswrapper.getdrawablefordensity 04-25 12:27:30.546 18000-18000/com.test.eyalcherevatzki.clientserver w/dalvikvm﹕ vfy: unable resolve virtual method 549: landroid/content/res/resources;.getdrawablefordensity (iilandroid/content/res/resources$theme;)landroid/graphics/drawable/drawable; 04-25 12:27:30.546 18000-18000/com.test.eyalcherevatzki.clientserver d/dalvikvm﹕ vfy: replacing opcode 0x6e @ 0x0002 04-25 12:27:30.646 18000-18000/com.test.eyalcherevatzki.clientserver i/adreno-egl﹕ <qegldrvapi_eglinitialize:410>: egl 1.4 qualcomm build: au_linux_android_lnx.la.3.5.2_rb1.04.04.02.065.012_msm8974_lnx.la.3.5.2_rb1__release_au () opengl es shader compiler version: e031.24.00.13 build date: 03/28/14 fri local branch: mybranch2893136 remote branch: quic/lnx.la.3.5.2_rb1 local patches: none reconstruct branch: au_linux_android_lnx.la.3.5.2_rb1.04.04.02.065.012 + nothing 04-25 12:27:30.676 18000-18000/com.test.eyalcherevatzki.clientserver d/openglrenderer﹕ enabling debug mode 0 04-25 12:27:30.736 18000-18000/com.test.eyalcherevatzki.clientserver i/timeline﹕ timeline: activity_idle id: android.os.binderproxy@41a99038 time:135458162 04-25 12:27:30.736 18000-18000/com.test.eyalcherevatzki.clientserver e/spannablestringbuilder﹕ span_exclusive_exclusive spans cannot have 0 length 04-25 12:27:30.736 18000-18000/com.test.eyalcherevatzki.clientserver e/spannablestringbuilder﹕ span_exclusive_exclusive spans cannot have 0 length 04-25 12:27:30.736 18000-18000/com.test.eyalcherevatzki.clientserver e/spannablestringbuilder﹕ span_exclusive_exclusive spans cannot have 0 length 04-25 12:27:30.736 18000-18000/com.test.eyalcherevatzki.clientserver e/spannablestringbuilder﹕ span_exclusive_exclusive spans cannot have 0 length 04-25 12:27:36.166 18000-18000/com.test.eyalcherevatzki.clientserver w/iinputconnectionwrapper﹕ showstatusicon on inactive inputconnection 04-25 12:27:39.986 18000-18000/com.test.eyalcherevatzki.clientserver e/spannablestringbuilder﹕ span_exclusive_exclusive spans cannot have 0 length 04-25 12:27:39.986 18000-18000/com.test.eyalcherevatzki.clientserver e/spannablestringbuilder﹕ span_exclusive_exclusive spans cannot have 0 length 04-25 12:27:39.986 18000-18000/com.test.eyalcherevatzki.clientserver e/spannablestringbuilder﹕ span_exclusive_exclusive spans cannot have 0 length 04-25 12:27:39.986 18000-18000/com.test.eyalcherevatzki.clientserver e/spannablestringbuilder﹕ span_exclusive_exclusive spans cannot have 0 length 04-25 12:27:41.256 18000-18000/com.test.eyalcherevatzki.clientserver w/iinputconnectionwrapper﹕ showstatusicon on inactive inputconnection 04-25 12:27:44.976 18000-18000/com.test.eyalcherevatzki.clientserver e/spannablestringbuilder﹕ span_exclusive_exclusive spans cannot have 0 length 04-25 12:27:44.986 18000-18000/com.test.eyalcherevatzki.clientserver e/spannablestringbuilder﹕ span_exclusive_exclusive spans cannot have 0 length 04-25 12:27:44.986 18000-18000/com.test.eyalcherevatzki.clientserver e/spannablestringbuilder﹕ span_exclusive_exclusive spans cannot have 0 length 04-25 12:27:44.986 18000-18000/com.test.eyalcherevatzki.clientserver e/spannablestringbuilder﹕ span_exclusive_exclusive spans cannot have 0 length 04-25 12:28:24.376 18000-18000/com.test.eyalcherevatzki.clientserver e/spannablestringbuilder﹕ span_exclusive_exclusive spans cannot have 0 length 04-25 12:28:24.376 18000-18000/com.test.eyalcherevatzki.clientserver e/spannablestringbuilder﹕ span_exclusive_exclusive spans cannot have 0 length 04-25 12:28:27.236 18000-18000/com.test.eyalcherevatzki.clientserver e/spannablestringbuilder﹕ span_exclusive_exclusive spans cannot have 0 length 04-25 12:28:27.236 18000-18000/com.test.eyalcherevatzki.clientserver e/spannablestringbuilder﹕ span_exclusive_exclusive spans cannot have 0 length 04-25 12:28:27.256 18000-18000/com.test.eyalcherevatzki.clientserver e/spannablestringbuilder﹕ span_exclusive_exclusive spans cannot have 0 length 04-25 12:28:27.256 18000-18000/com.test.eyalcherevatzki.clientserver e/spannablestringbuilder﹕ span_exclusive_exclusive spans cannot have 0 length
i added internet
permission.
i'm running server on drjava, problem?
i'm getting error: "ioexception: socket failed: access (permission denied)".
write down snippet below setcontentview()
if (android.os.build.version.sdk_int > 9) { strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy); }
or
class mytask extends asynctask<void, void, void> { @override protected void onpreexecute() { super.onpreexecute(); } @override protected void doinbackground(void... params) { // request } @override protected void onpostexecute(void result) { super.onpostexecute(result); } }
Comments
Post a Comment