1. Download our Official Android App: Forums for Android!

Apps Closed Socket

Discussion in 'Android Development' started by IatroJordan, Apr 13, 2016.

  1. IatroJordan

    IatroJordan Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    3
    Joined:
    Mar 23, 2016

    Mar 23, 2016
    3
    0
    5
    Male
    I have been trying to connect[​IMG] my application[​IMG] through a jdbc connection to my sql database. I understand that android 3.0 above, connection cannot be done on the UI itself and it is required to be run at the background. I used a thread to connect to the database and send information to the database before running the next activity. I have been receiving

    com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "Socket closed"

    Can anyone advice me on how to use a connect my application[​IMG]?

    Mainactivity

    public void onFinish() {

    try {
    Thread thread = new Thread(new Runnable(){
    String pId = "1",gId="40";
    int sNo=1,finalScore = 20,time = 40;
    @override
    public void run() {
    GameManager gameManager = new GameManager(pId,gId,sNo,finalScore,time);
    gameManager.GameMgrStart();

    }
    });
    thread start[​IMG]();
    FileOutputStream writer = openFileOutput("logs.txt", MODE_APPEND);
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(writer));
    out.write("Total touch on the screen: " + counterTouch);
    out.newLine();
    out.write("Total touch on the red circle: " + redTouch);
    out.newLine();
    out.write("Total Score: " + score);
    out close[​IMG]();
    } catch (Throwable t){
    }

    Intent intent = new Intent(GameActivity.this,EndActivity.class);
    intent.putExtra("score",tempScore);
    intent.putExtra("intCounter",counterTouch);
    startActivity(intent);
    }



    Gamemanager application:

    public class GameManager {
    /*
    //database connectors (local)
    String dbURL = "jdbc:sqlserver://localhost:1433;";
    String dbName = "databaseName=*****;";
    String dbSecurity = "integratedSecurity=true;";
    */

    //database connectors (server)
    String dbURL = "jdbc:sqlserver://******";
    String dbName = "databaseName=*****;";
    String dbUsername = "user=****;";
    String dbPassword = "password=*****;";

    String patientID;
    String gameID;
    int sqlNo;
    int score;
    int time;

    public GameManager(String pID, String gID, int sNo){
    patientID=pID;
    gameID = gID;
    sqlNo = sNo;
    }
    public GameManager(String pID, String gID, int sNo, int s, int t){
    patientID=pID;
    gameID = gID;
    sqlNo = sNo;
    score = s;
    time = t;
    }


    //This method is called to run Game[​IMG] Manager
    public void GameMgrStart(){
    Connection[​IMG] conn=null;
    String sqlStatement;
    try {
    conn = dbConnect();

    sqlStatement = sqlStats();

    //Run statement
    PreparedStatement cmdSql = conn.prepareStatement(sqlStatement);
    cmdSql.execute();

    System.out.println("" + sqlStatement);
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    }

    //List of SQL Statements
    public String sqlStats(){
    String sqlStatement= "";

    if(sqlNo == 1){
    sqlStatement = "INSERT INTO GameRecord(Score, timeTaken, AssignedGameID) Values('" + score + "', '" + time + "', (SELECT top 1 assignedGameID FROM assignedGame WHERE gameID='" + gameID + "' and patientAllocationID=(SELECT patientAllocationID FROM patientAllocation WHERE patientID='" + patientID + "')))";
    }

    return sqlStatement;
    }

    //Method to connect to database
    public Connection dbConnect(){
    Connection conn = null;
    try {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

    //conn = DriverManager.getConnection(dbURL+dbName+dbSecurity);
    conn = DriverManager.getConnection(dbURL+dbName+dbUsername+dbPassword);
    if (conn != null) {
    System.out.println("Connected");
    return conn;

    }
    else
    System.out.println("Failed");

    }
    catch (Exception e) {
    e.printStackTrace();
    }

    return conn;
    }

    public static void main(String [] args){
    GameManager objGM = new GameManager("1","1",1);
    objGM.GameMgrStart();
    }
    }
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,279
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,279
    10,378
    1,988
    Male
    Software developer
    South West of England
    Please use [code][/code] tags to format code
     

Share This Page

Loading...