1

我为我糟糕的英语提前道歉

该应用程序能够连接到服务器 java,但在交换数据时挂起。

这是客户端android代码:

@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 to 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 the credential");
        String resp = null;
        resp = in.readLine();
        Log.d(tag, "Receive the 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 to SSL Server", e);
    }
}


    public static String[] getCipherSuitesWhiteList(String[] cipherSuites) {
        List<String> whiteList = new ArrayList<>();
        List<String> rejected = new ArrayList<>();
        for (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 too small)
                    s.contains("krb5") || //reject Kerberos: unlikely to be used
                    s.contains("ssl") || //reject ssl (only tls)
                    s.contains("empty")) {    //not sure what this one is
                rejected.add(suite);
            } else {
                whiteList.add(suite);
            }
        }
        return whiteList.toArray(new String[whiteList.size()]);
    }}

我需要一个服务,因为我需要一个同步线程

这是java服务器代码:

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 for 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");
}}

交换数据(输入和输出)时,服务器和客户端都崩溃且没有消息错误

网络没问题,android app的权限是.INTERNET, .ACCESS_WIFI_STATE, .ACCESS_NETWORK_STATE

该服务器与 Java 桌面客户端配合良好。

非常感谢!

4

1 回答 1

0

尝试使用 Volley

https://developer.android.com/training/volley/index.html

您还可以将自定义请求与 volley 一起使用。

https://developer.android.com/training/volley/index.html

于 2014-11-19T12:02:29.203 回答