android cannot connect MYSQL server
Error Note:
04-11 04:01:51.283: W/System.err(913): com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
04-11 04:01:51.296: W/System.err(913): The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
04-11 04:01:51.296: W/System.err(913): at java.lang.reflect.Constructor.constructNative(Native Method)
04-11 04:01:51.304: W/System.err(913): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
MYSQL-DB Connection Code
private String CONNECTION_URL = "jdbc:mysql://192.168.2.10:8090/test";
private String user;
private String pass;
private java.sql.Statement stmt;
private java.sql.Connection conn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e(TAG, "step 1");
try {
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
Log.e(TAG, "step 2");
} catch (InstantiationException e) {
Log.e(TAG, "Error 1");
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
Log.e(TAG, "error 2");
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
Log.e(TAG, "error 3");
// TODO Auto-generated catch block
e.printStackTrace();
}
try {Log.e(TAG, "step 3");
conn = DriverManager.getConnection(CONNECTION_URL, user, pass);
} catch (SQLException e) {
Log.e(TAG, "error 4");
// TODO Auto-generated catch block
e.printStackTrace();
}
try {Log.e(TAG, "step 4");
stmt = conn.createStatement();
} catch (SQLException e) {Log.e(TAG, "error 5");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Related
I have the following code in JAVA to create Server Socket to listen for incoming connections :
public class ConnectOracle {
public static void main(String[] args) {
String driver = "oracle.jdbc.driver.OracleDriver"; //
String serverName = "10.11.201.84";
String portNumber = "1521";
String db = "XE";
String url = "jdbc:oracle:thin:#" + serverName + ":" + portNumber + ":"
+ db; // connectOracle is the data
// source name
String user = "ORAP"; // username of oracle database
String pwd = "ORAP"; // password of oracle database
Connection con = null;
ServerSocket serverSocket = null;
Socket socket = null;
DataInputStream dataInputStream = null;
DataOutputStream dataOutputStream = null;
try {
Class.forName(driver);// for loading the jdbc driver
System.out.println("JDBC Driver loaded");
con = DriverManager.getConnection(url, user, pwd);// for
// establishing
// connection
// with database
Statement stmt = (Statement) con.createStatement();
serverSocket = new ServerSocket(8888);
System.out.println("Listening :8888");
while (true) {
try {
socket = serverSocket.accept();
System.out.println("Connection Created");
dataInputStream = new DataInputStream(
socket.getInputStream());
dataOutputStream = new DataOutputStream(
socket.getOutputStream());
System.out.println("ip: " + socket.getInetAddress());
// System.out.println("message: " +
// dataInputStream.readUTF());
ResultSet res=stmt.executeQuery("select * from food_category");
while(res.next()){
System.out.println(res.getString(1));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (dataInputStream != null) {
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataOutputStream != null) {
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
In my Android program I am trying to create a connection with this JAVA program . The code is as follows :
class ConnectToOracle extends AsyncTask<String, String, String> {
Socket socket = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
#Override
protected void onPreExecute() {
super.onPreExecute();
;
}
// Download Music File from Internet
#Override
protected String doInBackground(String... f_url) {
try {
socket = new Socket("10.11.201.84", 8888);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream.writeUTF(textOut.getText().toString());
textIn.setText(dataInputStream.readUTF());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if (socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataOutputStream != null){
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataInputStream != null){
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
// Once Music File is downloaded
#Override
protected void onPostExecute(String file_url) {
// Dismiss the dialog after the Music file was downloaded
}
}
But the connection is not established . How can I establish connection ? Any advice is of great help .
I cant connect to the server i dont know why please help me. This is my code :
public class Sample extends Activity{
/** Called when the activity is first created. */
TextView tvHello;
XMPPTCPConnection connection;
ConnectionConfiguration config;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvHello = (TextView) findViewById(R.id.tvHello);
Log.i("ohyeah", "I'm here");
config = new ConnectionConfiguration("host", 5222, "servername");
connection = new XMPPTCPConnection(config);
try {
connection.connect();
// tvHello.setText("Connected to XMPP server");
Log.i("ohyeah", "Successfully Connected");
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("ohyeah", "Not Connected");
} catch (SmackException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("ohyeah", "Something Fishy");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("ohyeah", "yes");
}
}
}
this is my error:
http://i.stack.imgur.com/iaRdO.png
You can not do long or background running process in ui thread so try to use AsyncTask to connect xmpp server :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvHello = (TextView) findViewById(R.id.tvHello);
Log.i("ohyeah", "I'm here");
connectToXmppServer();
}
public void connectToXmppServer(){
new AsyncTask<Void,Void,String>(){
#Override
protected String doInBackground(Void... params) {
config = new ConnectionConfiguration("host", 5222, "servername");
connection = new XMPPTCPConnection(config);
try {
connection.connect();
// tvHello.setText("Connected to XMPP server");
Log.i("ohyeah", "Successfully Connected");
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("ohyeah", "Not Connected");
} catch (SmackException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("ohyeah", "Something Fishy");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("ohyeah", "yes");
}
return null;
}
}.execute();
}
External storage works, but not internal storage. Am I missing out on something fundamentally from the android storage mechanism?
thanks
public static void playSound()
{
//String path = internalPath + "/www/sounds/" + "SIREN.WAV"; // doesnt work
//String path = "file:///data/data/com.myproject.d08062014f/files/www/sounds/SIREN.WAV"; // doesnt work
//String path = "/data/data/com.myproject.d08062014f/files/www/sounds/SIREN.WAV"; // doesnt work
// all above does not work
Log.d("command", "command:" + path); // to check path string
String path = "file:///mnt/sdcard/media/audio/notifications/facebook_ringtone_pop.m4a"; // This one works!
MediaPlayer mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(path);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mMediaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mMediaPlayer.start();
}
Update on 09-10-2014
stackoverflow.com/a/4955915/856007 – Abdullah Shoaib Aug 12 at 7:49
thank you for the reference link below, I was able to get it working with
File file = new File(path); // acquire the file from path string
FileInputStream inputStream = new FileInputStream(file);
mMediaPlayer.setDataSource(inputStream.getFD());
inputStream.close();
I am trying to start browser action using Intent.ACTION_VIEW in onPostExecute() method of AsyncTask Class. But it is failing and giving below error
03-18 17:48:55.721: E/AndroidRuntime(26997): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
And when I add FLAG_ACTIVITY_NEW_TASK before startActivity() it doesn't crash but also no
action happens. Below is my code :
How to overcome this issue. Is this Context issue ?
private static class Task extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... arg0) {
CheckIfCertificateAvailable();
return null;
}
public void CheckIfCertificateAvailable() {
//Check for CertiDownload, OpenCerti, DeleteCerti & DeleteDirectory to be run only once when App installs for the first time
KeyStore ks = null;
try {
ks = KeyStore.getInstance("AndroidCAStore");
} catch (KeyStoreException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
ks.load(null, null);
} catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (CertificateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Enumeration aliases = null;
try {
aliases = ks.aliases();
} catch (KeyStoreException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement().toString();
X509Certificate cert = null;
try {
cert = (X509Certificate)
ks.getCertificate(alias);
} catch (KeyStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(cert.getSubjectDN().getName().contains("CN=vpn.origin.mediainsiderspanel.com,OU=IT,O=Symphony AM,L=PA,ST=CA,C=US")){
prefs.setCertiStatus(context, false);
Log.d("DManager", "Certificate already exist no installation required - Flag False");
break;
}
else
{
prefs.setCertiStatus(context, true);
Log.d("DManager", "Certificate does not exist will have to install - Flag True");
}
}
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
//prefs.setCertiStatus(context, prefs.getCertiStatus(context));
Log.d("DManager", "onPostExecute - Flag : " + prefs.getCertiStatus(context));
if(prefs.getCertiStatus(context)){
Log.d("DManager", "Now Installing");
Uri uri = Uri.parse("http:url");
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setData(uri);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
else {
Log.d("DManager", "Return");
return;
}
super.onPostExecute(result);
}
}
got my code working. Just having a little trouble with one part.
public void picksound(){
Intent mIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(mIntent, 1);
}
public void onActivityResult(int requestCode, int resultCode, Intent mIntent) {
if (resultCode == RESULT_OK && null != mIntent) {
if (requestCode == 1) {
Uri selectedRing = mIntent.getData();
}
}}
private void playSong(String selectedRing){
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(selectedRing);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
}
public void alarmmsg(){
//ringtone player
playSong(selectedRing);
//
So that's the code I am working with. When I try to call the playSong function, I am asked for a string, and the only string it seems to accept is "null". Can someone help to see what I've done wrong?
Try changing this line :
MediaPlayer mp = new MediaPlayer();
To this:
MediaPlayer player = MediaPlayer.create(this, Uri.parse("http://www.urltofile.com/file.mp3"));