Java SocketException Error - java

I'm trying to understand how an API works to develop using it.
I need to connect in that API and authenticate.
The authentication method is POST.
Here is my code:
try {
URL url = new URL("http://api.olhovivo.sptrans.com.br/v0/Login/Autenticar");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.addRequestProperty("token", "77b495072cf8b35a0b187218721871e4790dd6b92asxald6a29");
int code = connection.getResponseCode();
System.out.println("Response Code: " + code);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Here is the error I get:
java.net.SocketException: Permission denied: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at TestandoAPIOlhoVivo.main(TestandoAPIOlhoVivo.java:19)
What am I doing wrong?

Related

"java.sql.SQLException: No database selected" with MySQL JDBC [duplicate]

This question already exists:
Error:java.sql.SQLSyntaxErrorException: Table 'loginpass.intousers' doesn't exist [duplicate]
Closed 3 years ago.
Code:
Connection dbConnection;
public Connection getDbConnection() throws ClassNotFoundException, SQLException{
String connectionString = "jdbc:mysql://127.2.0.1:3306/?user=root?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false";
;
String url="?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
Class.forName("com.mysql.jdbc.Driver");
dbConnection = DriverManager.getConnection(connectionString, dbUser, dbPass);
return dbConnection;
}
public void SingUpUser(String id,String login, String password) {
String insert = "INSERT INTO"+Constant.USER_TABLE+"("+Constant.USERS_ID+","+Constant.USERS_LOGIN+","+Constant.USER_PASSWORD+")"+
"VALUES(?,?,?)";
try {
PreparedStatement prSt = getDbConnection().prepareStatement(insert);
prSt.setString(1, id);
prSt.setString(2, login);
prSt.setString(3, password);
prSt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Error:
java.sql.SQLException: No database selected
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:955)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1094)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1042)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1345)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1027)
at minelaunch.Databases.SingUpUser(Databases.java:36)
at minelaunch.minelauncherv1$5.actionPerformed(minelauncherv1.java:182)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
I expect to write to the database, but I get this error.
From the stack trace:
java.sql.SQLException: No database selected
You have removed the database name from your connection string, so now MySQL does not know which database you want to work with. Edit your connection string again and specify the actual name of the database you want to use.

Connecting RESTful client to local virtual machine

I am trying to connect my desktop Windows 10 machine with local Virtual Machine having Ubuntu 16.04.
I have written a RESTful client in following way to connect to this machine in following way.
public class NetClientGet {
// http://localhost:8080/RESTfulExample/json/product/get
public static void main(String[] args) {
try {
//URL url = new URL("http://localhost:8080/RESTfulExample/json/product/get");
URL url = new URL("http://192.168.56.101:8998/sessions");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I am able to ssh into this virtual machine via Putty.The peculiarity is that till I don't ssh via Putty in this virtual machine, this code produces error as follows:
java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at com.mkyong.rest.client.NetClientGet.main(NetClientGet.java:22)
So what is actually missing in my approach of making connection?

Java sqlite database is locked

I am trying to delete a student from database, but I got this error:
java.sql.SQLException: database is locked
at org.sqlite.core.DB.throwex(DB.java:859)
at org.sqlite.core.DB.exec(DB.java:142)
at org.sqlite.jdbc3.JDBC3Connection.commit(JDBC3Connection.java:165)
at Controller.deleteStudent(Controller.java:232)
at Test$9.actionPerformed(Test.java:227)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
I read other posts on stackoverflow and all of them said that all connections needs to be closed to open a new connection. I did this, but I still got the same problem.
Here's my code:
public static void deleteStudent(int id) {
try {
con = null;
Class.forName("org.sqlite.JDBC");
con = DriverManager.getConnection("jdbc:sqlite:test.db");
con.setAutoCommit(false);
stmt = null;
stmt = con.createStatement();
int result = stmt.executeUpdate("DELETE FROM STUDENTS WHERE ID=" + id + ";");
System.out.println(result);
con.commit();
stmt.close();
con.close();
output();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
Do you have ideas why I still get this error?

Java calls php to retrieve SQL data

I'm hoping someone can help figure this out. I've got a problem trying to run some backend php code from java that extracts sql data and sends it back via gson. I've used posting from java to php and inserting it into mysql database as a model, but, I can't seem to get beyond the HTTP 400 response code that is returned. I've edited the following java console to remove identifying names, but, the context should remain the same.
security: JSS is not configured
network: Connecting https://secure.justhost.com/<server>/<folder>/<php program name>.php with proxy=DIRECT
network: Cache entry not found [url: http://secure.justhost.com:443/crossdomain.xml, version: null]
network: Connecting http://secure.justhost.com:443/crossdomain.xml with proxy=DIRECT
network: Connecting http://secure.justhost.com:443/ with proxy=DIRECT
java.io.IOException: Server returned HTTP response code: 400 for URL: http://secure.justhost.com:443/crossdomain.xml
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at com.sun.deploy.net.CrossDomainXML.check(Unknown Source)
at com.sun.deploy.net.CrossDomainXML.check(Unknown Source)
at sun.plugin2.applet.SecurityManagerHelper.checkConnectHelper(Unknown Source)
at sun.plugin2.applet.AWTAppletSecurityManager.checkConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
at newposting.GetCategories(newposting.java:864)
at newposting.<init>(newposting.java:170)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter$1.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
basic: exception: java.lang.reflect.InvocationTargetException.
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.runOnEDTAndWait(Unknown Source)
at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.instantiateApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.initAppletAdapter(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at com.sun.deploy.uitoolkit.impl.awt.OldPluginAWTUtil.invokeAndWait(Unknown Source)
... 5 more
Caused by: java.lang.RuntimeException: java.security.AccessControlException: access denied ("java.net.SocketPermission" "secure.justhost.com:443" "connect,resolve")
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getHeaderField(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
at newposting.GetCategories(newposting.java:864)
at newposting.<init>(newposting.java:170)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter$1.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.security.AccessControlException: access denied ("java.net.SocketPermission" "secure.justhost.com:443" "connect,resolve")
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkConnect(Unknown Source)
at sun.plugin2.applet.SecurityManagerHelper.checkConnectHelper(Unknown Source)
at sun.plugin2.applet.AWTAppletSecurityManager.checkConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
... 29 more
security: Reset deny session certificate store
The url for the php code has no spaces. It does use underlines ("_") to join words. The request that is show in the console does not pass any parameters to the url because the php code has all it needs to retrieve the data. The java code is simply calling the backend php code. Here's a look at what I've got for the java code.
private ArrayList<String> GetCategories ()
{
ArrayList<String> cats = new ArrayList<String> ();
// preparation
String charset = "UTF-8";
String szURL = new String ();
if (!debugConfiguration)
szURL = "https://secure.justhost.com/<server>/<folder>/<php program name>.php";
else
szURL = "http://localhost/<php program name>.php";
// HTTP Post request
URL object = null;
try
{
object = new URL(szURL);
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpsURLConnection con = null;
try
{
con = (HttpsURLConnection) object.openConnection();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//add request header
try
{
con.setRequestMethod("POST");
}
catch (ProtocolException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty(charset, "utf-8");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
// Send post request
//con.setDoOutput(true);
/*
DataOutputStream wr = null;
try
{
wr = new DataOutputStream (con.getOutputStream());
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
wr.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
try
{
int responseCode = con.getResponseCode();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader in = null;
try
{
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
String inputLine;
StringBuffer response = new StringBuffer();
try
{
while ((inputLine = in.readLine()) != null)
{
response.append(inputLine);
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
in.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
Gson gson = new Gson ();
jsonArticleCategories[] enums = gson.fromJson(response.toString(), jsonArticleCategories[].class);
for (int i=0; i<enums.length; ++i)
{
jsonArticleCategories jac = enums[i];
cats.add(jac.get_category());
}
return cats;
}
Again, I've edited the code sample to remove some names, but the context is valid. There are not spaces in any of the names for the server, folder, or program name. Lastly, let me add that the java code is digitally signed with a code signing certificate and run through jnlp. The php code runs correctly if executed manually from a browser.
Thanks for any help.

How to pass a jFrame with jTable via sockets?

I have a client-server application, in the client side i have a button to recieve a frame
with table inside it from the server.
If i pass the frame with other jComponents (JButton,JTextField) it works fine, but when i'm
tryin to pass the frame with a jTable i'm getting null exception in the client side.
Here is my code:
Client side:
private class GetServerData extends Thread
{
String server_msg = " ";
Socket the_client;
ObjectInputStream from_server;
public GetServerData(Socket client)
{
the_client = client;
try {
from_server = new ObjectInputStream(the_client.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run()
{
do {
try {
Object obj = from_server.readObject(); // this is the line when the exception reference to
// when trying to read the JFRAME
if (obj instanceof JFrame) {
JFrame window = (JFrame)obj;
window.setVisible(true);
window.pack();
}
else {
server_msg = (String)obj;
System.out.println(server_msg);
}
}
catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
}while(!server_msg.equals("bye"));
}
}
Server Side:
public void run(){
while (true){
try {
data_from_client = (Vector)from_client.readObject();
if (data_from_client.elementAt(0)equals("string")) {
String s = "Hello user";
to_client.writeObject(s);
to_client.flush();
}
else if (data_from_client.elementAt(0).equals("table")) {
String [][]d = {{"yoyo","jojo"},{"koko","momo"}};
String []h = {"name","best friend"};
JTable jtable = new JTable(d,h);
JScrollPane scroll = new JScrollPane(jtable);
JPanel panel = new JPanel();
panel.add(scroll,BorderLayout.CENTER);
JFrame frame = new JFrame("Im from the server!!");
frame.add(panel);
to_client.writeObject(frame);
to_client.flush();
}
//else if (data_from_client.elementAt(0).equals("bye")) {
// to_client.println("bye");
// to_client.flush();
// socket.close();
// socket = null;
//}
}
catch(IOException | ClassNotFoundException ioe) {
break;
// error in reading streams from client
}
}
close();
}
The exception is:
Exception in thread "Thread-21" java.lang.NullPointerException
at java.awt.Container.readObject(Unknown Source)
at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readArray(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.access$300(Unknown Source)
at java.io.ObjectInputStream$GetFieldImpl.readFields(Unknown Source)
at java.io.ObjectInputStream.readFields(Unknown Source)
at java.awt.Container.readObject(Unknown Source)
at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readArray(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.access$300(Unknown Source)
at java.io.ObjectInputStream$GetFieldImpl.readFields(Unknown Source)
at java.io.ObjectInputStream.readFields(Unknown Source)
at java.awt.Container.readObject(Unknown Source)
at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readArray(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.access$300(Unknown Source)
at java.io.ObjectInputStream$GetFieldImpl.readFields(Unknown Source)
at java.io.ObjectInputStream.readFields(Unknown Source)
at java.awt.Container.readObject(Unknown Source)
at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readArray(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.access$300(Unknown Source)
at java.io.ObjectInputStream$GetFieldImpl.readFields(Unknown Source)
at java.io.ObjectInputStream.readFields(Unknown Source)
at java.awt.Container.readObject(Unknown Source)
at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readArray(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.access$300(Unknown Source)
at java.io.ObjectInputStream$GetFieldImpl.readFields(Unknown Source)
at java.io.ObjectInputStream.readFields(Unknown Source)
at java.awt.Container.readObject(Unknown Source)
at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readArray(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.access$300(Unknown Source)
at java.io.ObjectInputStream$GetFieldImpl.readFields(Unknown Source)
at java.io.ObjectInputStream.readFields(Unknown Source)
at java.awt.Container.readObject(Unknown Source)
at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at pack.connect_to_server.ServerConnection$GetServerData.run(ServerConnection.java:92)
I've commented in the client side, where the exception occurs.
Passing JFrames (or any Window for that matter) via Serialization is a bad idea, in my experience, this seems to come down to the connection it has to the systems native peer, once transferred, this is lost and generally causes lots of nasty problems, in my experience.
I could be missing really obvious and some has over come these limitations, but I've not found away.
If you can, you should only transfer the data content instead, it's generally safer and a lot less messy ;)

Categories