java - App connects to the server but can not communicate -
i apologize in advance bad english
the app able connect server java, hangs @ time of exchange of data.
this client android code:
@suppresslint("newapi") public class connection extends intentservice{ private string tag = "ciclo eventi"; private string user; private string pass; public connection() { super("connection"); } public void oncreate(){ super.oncreate(); strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy); } public void onstart(intent intent, int startid){ log.d(tag, "getdata"); bundle extras = intent.getextras(); user = (string) extras.get("user"); pass = (string) extras.get("password"); log.d(tag, user); log.d(tag, pass); onhandleintent(intent); } public int onstartcommand(intent intent, int flags, int startid){ onhandleintent(intent); return start_not_sticky; } @override public void ondestroy() { log.d(tag, "connection closed"); } @override protected void onhandleintent(intent intent) { socket s=null; bufferedreader in=null; printwriter writer=null; try { log.d(tag, "try connect"); s = getconnection("192.168.1.103", 5433); log.d(tag, "connection done"); in = new bufferedreader(new inputstreamreader(s.getinputstream())); writer = new printwriter(s.getoutputstream(), true); writer.println(user); writer.println(pass); log.d(tag, "i've send credential"); string resp = null; resp = in.readline(); log.d(tag, "receive results"); if(resp.equals("done")){ log.d(tag, "accept"); /*intent i=new intent(this,secondactivity.class); startactivity(i);*/ ondestroy(); } else{ log.d(tag, "refused"); } } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } protected socket getconnection(string ip, int port) throws ioexception { try { keystore truststore = keystore.getinstance("bks"); inputstream truststorestream = getapplicationcontext().getresources().openrawresource(r.raw.server); truststore.load(truststorestream, "keypass".tochararray()); trustmanagerfactory trustmanagerfactory = trustmanagerfactory.getinstance(keymanagerfactory.getdefaultalgorithm()); trustmanagerfactory.init(truststore); sslcontext sslcontext = sslcontext.getinstance("tls"); sslcontext.init(null, trustmanagerfactory.gettrustmanagers(), null); sslsocketfactory factory = sslcontext.getsocketfactory(); sslsocket socket = (sslsocket) factory.createsocket(ip, port); //socket.setenabledciphersuites(getciphersuiteswhitelist(socket.getenabledciphersuites())); return socket; } catch (generalsecurityexception e) { log.e(this.getclass().tostring(), "exception while creating context: ", e); throw new ioexception("could not connect ssl server", e); } } public static string[] getciphersuiteswhitelist(string[] ciphersuites) { list<string> whitelist = new arraylist<>(); list<string> rejected = new arraylist<>(); (string suite : ciphersuites) { string s = suite.tolowercase(); if (s.contains("anon") || //reject no anonymous s.contains("export") || //reject no export s.contains("null") || //reject no encryption s.contains("md5") || //reject md5 (weaknesses) s.contains("_des") || //reject des (key size small) s.contains("krb5") || //reject kerberos: unlikely used s.contains("ssl") || //reject ssl (only tls) s.contains("empty")) { //not sure 1 rejected.add(suite); } else { whitelist.add(suite); } } return whitelist.toarray(new string[whitelist.size()]); }}
i need service because need syncronous thread
this java server code:
public class socketthread implements runnable{ private socket s1; private bufferedreader in; private printwriter out; private string user = "admin"; private string pass = "ciao"; socketthread(socket s){ this.s1=s; } public void run(){ boolean logindone = false; string user1 = null; string password1 = null; string lati = null; string longi = null; string via = null; system.out.println("connected"); try { in = new bufferedreader(new inputstreamreader(s1.getinputstream())); out = new printwriter(s1.getoutputstream(), true); user1 = in.readline(); password1 = in.readline(); system.out.println("user : "+user1+" password : "+ password1); } catch (ioexception e) { // todo auto-generated catch block system.out.println("error on buffer creation"); e.printstacktrace(); } system.out.println("access done, wait check credential"); do{ if(user1.compareto(user)==0 && password1.compareto(pass)==0){ logindone = true; out.println("done"); } else{ out.println("noaccess"); } }while(logindone == false); system.out.println("login done"); try { system.out.println("close done"); s1.close(); in.close(); out.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } system.out.println("thread off"); }}
both server , client crashes no message error when exchanging data (both input , output)
the network ok, privileges of android app .internet, .access_wifi_state, .access_network_state
the server works java desktop client.
thank much!
try using volley
https://developer.android.com/training/volley/index.html
you can use custom requests volley.
Comments
Post a Comment