Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying connecting my android app to postgres database on server
    text
    copied!<p>I am trying to connect my android app to a postgres database running on server, but i am having problem.</p> <p>Following is my code, </p> <pre><code>@TargetApi(Build.VERSION_CODES.GINGERBREAD) private void download() throws IOException { // TODO Auto-generated method stub StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() .permitAll().build(); StrictMode.setThreadPolicy(policy); int response = -1; InputStream in = null; try { Class.forName("org.postgresql.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); text.setText((CharSequence) e);} try { HttpURLConnection conn1; String urli = "jdbc:postgresql://"my ip":5432/iwmp_prd?user=postgres"; conn1 = (HttpURLConnection) DriverManager.getConnection(urli); URL url = new URL(urli); //HttpURLConnection conn1 = url.openConnection(); conn1.connect(); if (!(conn1 instanceof HttpURLConnection)) throw new IOException("Not an HTTP connection"); //HttpURLConnection httpConn = (HttpURLConnection) conn1; //httpConn.setAllowUserInteraction(false); //httpConn.setInstanceFollowRedirects(true); //httpConn.setRequestMethod("GET"); //httpConn.connect(); response = conn1.getResponseCode(); if (response == HttpURLConnection.HTTP_OK) { in = conn1.getInputStream(); try { String nsql = "select user_name from iwmp_user_reg where user_id=?"; PreparedStatement pstm = ((Connection) conn1).prepareStatement(nsql); String user = "DL0000AD"; pstm.setString(1, user); ResultSet rs = pstm.executeQuery(); while (rs.next()) { String retval = rs.getString(1); text.setText((CharSequence) retval); } rs.close(); pstm.close(); ((Closeable) conn1).close(); } catch (SQLException e) { e.printStackTrace(); text.setText((CharSequence) e); } } } catch (Exception ex) { throw new IOException("Error connecting"); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } </code></pre> <p>Errors as follows,</p> <pre><code>java.lang.ClassCastException: org.postgresql.jdbc2.Jdbc2Connection cannot be cast to java.net.HttpURLConnection and it i use HttpURLConnection, then i get an error of java.net.MalformedURLException: Unknown protocol: jdbc </code></pre> <p>I know my code is all messed up, but still i m trying.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload