Closing Jsoup Connection - java

I need a little help understanding the basics of Jsoup. The following code works but I'm wondering if the connection needs to be closed somehow. Can't find anything on the Jsoup website about it. If the application remains untouched after the do in background method executes I get a message in log cat every five minutes or so saying "request time failed: java.net.SocketException: Address family not supported by protocol". So I want to make sure I'm not unnecessarily consuming data. Thank you.
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try {
// connect to web page based on user input
Document doc = Jsoup.connect(routeURL).get();
// select relevant page elements
Elements fareStageNumbers = doc.getElementsByClass("fare_stages_inner_table");
// test printing out fare stage numbers
for(Element div : fareStageNumbers){
Log.v(TAG, div.text());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
logcat message:
01-12 20:58:28.755: D/SntpClient(78): request time failed: java.net.SocketException: Address family not supported by protocol
01-12 21:03:28.765: D/SntpClient(78): request time failed: java.net.SocketException: Address family not supported by protocol
01-12 21:08:28.775: D/SntpClient(78): request time failed: java.net.SocketException: Address family not supported by protocol

Jsoup closes the connection by its own, after the request is done:
// from 'org.jsoup.helper.HttpConnection' class
static HttpConnection.Response execute(Connection.Request req, HttpConnection.Response previousResponse) throws IOException {
// ...
HttpURLConnection conn = createConnection(req);
HttpConnection.Response res;
try {
// ...
} finally {
// per Java's documentation, this is not necessary, and precludes keepalives. However in practise,
// connection errors will not be released quickly enough and can cause a too many open files error.
conn.disconnect();
}
// ...
}
Exception: Does your url contain the protocol (the url start with eg. http://)?

Related

Jersey UniformInterfaceException when doing a POST with docker-client

When trying to post a RESTful Service via docker-client to my private docker registry i get that error. The confusing thing about that is that the input stream changes itself as you can see here. Its made somewhere in the background of jersey but i cannot find the cause for it. I guess the problem occurs somewhere inside the jersey logic
Method
public static Service initService(String imageId) {
final com.spotify.docker.client.DockerClient docker = new DefaultDockerClient(
"http://10.###.###.143:2375");
String s = null;
try {
s = ("10.###.###.143:5000/user/ipatest&tag=latest");
docker.pull(s);
System.out.println(docker.toString());
} catch (DockerException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Exception
com.spotify.docker.client.DockerRequestException: Request error: POST http://10.###.###.143:2375/v1.12/images/create?fromImage=10.###.###.143%3A5000%2Fuser%2Fipatest%26tag%3Dlatest: 500
at com.spotify.docker.client.DefaultDockerClient.propagate(DefaultDockerClient.java:563)
at com.spotify.docker.client.DefaultDockerClient.request(DefaultDockerClient.java:544)
at com.spotify.docker.client.DefaultDockerClient.pull(DefaultDockerClient.java:345)
at com.spotify.docker.client.DefaultDockerClient.pull(DefaultDockerClient.java:329)
at de.fhg.ipa.vfk.eapps.commoniaas.docker.DockerServiceMgmt.initService(DockerServiceMgmt.java:43)
at de.fhg.ipa.vfk.eapps.commoniaas.docker.DockerServiceMgmt.main(DockerServiceMgmt.java:163)
Caused by: com.sun.jersey.api.client.UniformInterfaceException: POST http://10.###.###.143:2375/v1.12/images/create?fromImage=10.###.###.143%3A5000%2Fuser%2Fipatest%26tag%3Dlatest returned a response status of 500 Internal Server Error
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:688)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.method(WebResource.java:623)
at com.spotify.docker.client.DefaultDockerClient.request(DefaultDockerClient.java:540)
... 4 more
It must have something to do with queryParams(String params) which belongs to WebResource (jersey)
The image name you're passing to pull isn't valid. The format for an image name is name:tag. Do this instead:
docker.pull("10.###.###.143:5000/user/ipatest:latest");
Alternatively, you can ommit the :latest, since it is implied when no other tag is specified.

nullpointerexception error. trying to connect to the bitcoin server from my phone

I am fairly new to android, java, and mainly jsonrpc...
I have been stuck on this error for some time and i dont know if it is how i have coded it that is causing the issue or if i am trying to connect to the wrong server...
If you could help, it would be GREATLY appreciated.
here is the error code and the important code:
09-08 12:36:12.141: W/System.err(30361): Network exception: failed to connect to /10.10.11.75 (port 18332): connect failed: ECONNREFUSED (Connection refused)
09-08 12:36:12.151: W/dalvikvm(30361): threadid=11: thread exiting with uncaught exception (group=0x417df2a0)
09-08 12:36:12.151: E/AndroidRuntime(30361): FATAL EXCEPTION: AsyncTask #1
09-08 12:36:12.151: E/AndroidRuntime(30361): java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException
at com.bitcoinapp.MainActivity.connect(MainActivity.java:219
at com.bitcoinapp.MainActivity.access$0(MainActivity.java:157)
at com.bitcoinapp.MainActivity$BitcoinConnect.doInBackground(MainActivity.java:143)
at com.bitcoinapp.MainActivity$BitcoinConnect.doInBackground(MainActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-08 12:36:12.151: E/AndroidRuntime(30361): ... 5 more
this is the android code that i am using at the moment... all of which is run once the user presses the button on the application. I am working off my SAMSUNG S3 MINI and not an emulator.
I have had to omit some of the content, so showing line numbers wont help, but i have added in the ones that i think are important...
class Connection implements OnClickListener {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
new BitcoinConnect().execute();
}
}
private class BitcoinConnect extends AsyncTask<String, Integer, Object[]> {
#Override
protected Object[] doInBackground(String... params) {
// TODO Auto-generated method stub
connect();
return null;
}
}
public class BasicAuthenticator implements ConnectionConfigurator {
public void configure(HttpURLConnection connection) {
// add custom HTTP header
connection.addRequestProperty("myusername", "mypassword");
}
}
private void connect() { //(line 157)
//This is the code for the JSONRPC2-CLIENT iteraction
// The JSON-RPC 2.0 server URL
URL serverURL = null;
try {
serverURL = new URL("http://10.10.11.75:18332");
} catch (MalformedURLException e) {
// handle exception...
e.printStackTrace();
}
// Create new JSON-RPC 2.0 client session
JSONRPC2Session mySession = new JSONRPC2Session(serverURL);
mySession.setConnectionConfigurator(new BasicAuthenticator());
//This is for the bitcoin BASE interaction.
//for the Bitcoin Payment Request
String method = "getinfo";
Map<String,Object> params = new HashMap<String,Object>();
String id = "Request001";
JSONRPC2Request payment = new JSONRPC2Request(method, params, id);
String jsonString = payment.toString();
JSONRPC2Response response = null;
Log.i("Failed0", "Failed0");
try {
response = mySession.send(payment);
Log.i("response", String.valueOf(response));
mTextView.setText((CharSequence) response);
} catch (JSONRPC2SessionException e) {
System.err.println(e.getMessage());
// handle exception...
Log.i("response", String.valueOf(response));
}
// Print response result / error
if (response.indicatesSuccess()) { //(line 219)
System.out.println(response.getResult());
} else {
System.out.println(response.getError().getMessage());
}
}
I am using the JSONRPC2.0 libraries for the base code and the client code. I am trying to connect to the bitcoin testnet server. The IP there is one of many that i have tried...
I know that testnet is on port 18332 and the mainnet is 8332...
ive tried a localhost IP and that hasnt worked either.
I dont know if there is suppose to be other code that i must use in order for my phone to connect to the bitcoin server...
Please help me, thanks in advance
Lets apply some logical thinking.
According to the stack trace, the NullPointerException was thrown here:
if (response.indicatesSuccess()) {
That means response was null. (No other alternative!)
That means that the send call in
response = mySession.send(payment);
EITHER returned assigned null to response, OR it through a JSONRPC2SessionException that you caught.
I suspect it was the latter, and that the message "Network exception: failed to connect to /10.10.11.75 (port 18332): ..." was logged in the process. However, the evidence is not convincing. (There aren't any "I/..." lines in the logcat output ...)
In summary, what you have done is catch the exception that told you that the send has failed, and then proceed to try to process the non-existent response!
The underlying problem is that you app cannot connect to 10.10.11.75 on port 18332. I assume that you realize that 10.10.11.75 is a private IP address, and hence that you will only be able to connect to it if the server is on your local network.

Java HTTP Request Fail

I'm doing one java query with http on some search engines and here is the code of two classes:
public EventSearch(){
btsearch.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==btsearch){
try {
HttpRequest http = new HttpRequest(CatchQuery());
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "HTTP request failure.");
}
this.dispose();
}
}
public String CatchQuery(){
query=txtsearch.getText();
return query;
}
and
public class HttpRequest extends EventSearch
{
String query;
URL url;
public HttpRequest(String query) throws IOException{
// Fixed search URL; drop openConnection() at the end
try {
url = new URL("http://google.com/search?q="+query);
System.out.println(CatchQuery());
} catch (MalformedURLException e) {
JOptionPane.showMessageDialog(null, "Unable to search the requested URL");
}
// Setup connection properties (this doesn't open the connection)
URLConnection connection = url.openConnection();
connection.setRequestProperty("Accept-Charset", "UTF-8");
// Setup a reader
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
// Read line by line
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println (line);
}
// Close connection
reader.close();
}
The thing is - There are no errors regarding the code but the request is stucked. I don't receive any sort of message on my console our debug. I'm thinking of any sort of memory error since I'm working with strings but anyone has any idea of whats going wrong on?
thank you
Edit One:
public String CatchQuery(){
query=txtsearch.getText();
return query;
}
CatchQuery Simple catch the query of the txtsearch (field).
Edit Two: [Topic Solved]
Two problems:
"http://google.com/search?q="+query should be "http://google.com/search?q="+URLEncoder.encode(query), query url needs to be encoded before opening a connection, so that unsupported characters are converted to url-friendly characters
Google does not accept bot connections, you should use the Google Java API to perform searches properly
UPDATE
Google does not accept connections without the User Agent header, so you have to edit the HttpRequest class to set the user agent after creating the connection:
// Setup connection properties (this doesn't open the connection)
URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux x86_64; en-GB; rv:1.8.1.6) Gecko/20070723 Iceweasel/2.0.0.6 (Debian-2.0.0.6-0etch1)");
connection.setRequestProperty("Accept-Charset", "UTF-8");
It works for me, test it and tell me if it works for you too.
Note: from Google ToS:
Automated queries
Google's Terms of Service do not allow the sending of automated queries of any sort to our system without express permission in advance from Google. Sending automated queries consumes resources and includes using any software (such as WebPosition Gold) to send automated queries to Google to determine how a website or webpage ranks in Google search results for various queries. In addition to rank checking, other types of automated access to Google without permission are also a violation of our Webmaster Guidelines and Terms of Service.

Client Exception: Software caused connection abort: recv failed

I've been playing around with my Java client app. Behaviour is very similar to this post (and others):
java.net.SocketException: Software caused connection abort: recv failed
As yet I've not found the answer - hence this post. I have a idea what is wrong but I'm not sure what to do about it.
The protocol is simple - read some data in, send back an ACK and repeat (until terminted).
I've been looking at what is going on with WireShark, and is seems the TCP window is filling up. I'm performing a flush() on the DataOutputStream (For the ACKs) but doesn't change the fact at after a while I get this exception (I can see on WireShark that there is always a window problem right before the Java exception).
So how to I make sure in Java that my TCP windows/buffers are clear (Which I think is the root cause of the problem?) seems to be no flush on the DataInputStream. Make be wonder that whilst I might be reading it the TCP stack is filling up.
Many Thanks
Mark
I've attached the basic code calls below:
public void connectToServer()
{
//Create socket connection
try
{
if (lSocket == null)
{
lSocket = new Socket("localhost", 7651);
lSocket.setSoTimeout(0);
lInDataStream = new DataInputStream(lSocket.getInputStream());
lOutDataStream = new DataOutputStream(lSocket.getOutputStream());
}
}
catch (UnknownHostException e)
{
System.out.println("Unknown host: localhost");
}
catch (IOException e)
{
System.out.println("No I/O");
}
}
public void readSocket() throws IOException
{
//Receive data from ROS SerialtoNetwork server
if (lSocket != null)
{
lInDataStream.readInt();
lInDataStream.readFully(cbuf);
lOutDataStream.writeBytes("Ack");
lOutDataStream.flush();
//String lstr = new String(cbuf);
//System.out.print(lstr);
//System.out.println("");
}
}
public String getDataBuffer()
{
String lstr = new String(cbuf);
return lstr;
}
This indicates persistent network errors. There are several (repetitive) MSDN article on this error, e.g. this one.

WCF Service, Java JApplet client, transport error 405

I'm having a problem with a WCF Service and Java Client, I will try to give as much information as i can, thanks for your time.
The Endpoint of the server is BasicHttpBinding, I tried hosting the server as a Windows Service and in IIS but nothing changed.
The weird thing is that the Client works great if I use a simple class, in the moment I switch the class to an JApplet I get the problem mentioned.
I'm using Eclipse as an IDE, I tried Axis and Metro to generate the stub with the same bad results.
Here is an example of the Java class where everything is working
public class TestSoaMetro {
public String TestMethod(){
String result = null;
IDigitalSignatureService aa = new DigitalSignatureService().getBasicHttpBindingEndpoint();
try {
result = aa.getData("1", "id002962");
} catch (IDigitalSignatureServiceGetDataArgumentExceptionFaultFaultMessage e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IDigitalSignatureServiceGetDataInvalidOperationExceptionFaultFaultMessage e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
Here is the example of the JApplet where I get the error:
public class TestSoaMetroApplet extends JApplet {
public void init() {
Container content = getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
String result= this.TestMethod();
JLabel label = new JLabel(result);
content.add(label);
}
public String TestMethod(){
String result = null;
IDigitalSignatureService aa = null;
try {
aa = new DigitalSignatureService().getBasicHttpBindingEndpoint();
result= aa.getData("1", "id002962");
} catch (IDigitalSignatureServiceGetDataArgumentExceptionFaultFaultMessage e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IDigitalSignatureServiceGetDataInvalidOperationExceptionFaultFaultMessage e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
In the moment the Applet loads I get the error, is the exact same call so I don't understand why I get the exception using the Applet. I Also tried to call this from a Silverlight client and I was getting a security exception, this is where I found out about clientaccesspolicy.xml and crossdomain.xml, I added clientaccesspolicy.xml to the service and the Silverlight Client works great, so I decided to try crossdomain.xml and nothing, the Applet still does not work.
I will put the stack trace at the end, thanks all for your time.
Juan Zamudio
javax.xml.ws.WebServiceException: org.apache.axis2.AxisFault: Transport error: 405 Error: Method not allowed
at org.apache.axis2.jaxws.ExceptionFactory.createWebServiceException(ExceptionFactory.java:175)
at org.apache.axis2.jaxws.ExceptionFactory.makeWebServiceException(ExceptionFactory.java:70)
at org.apache.axis2.jaxws.ExceptionFactory.makeWebServiceException(ExceptionFactory.java:128)
at org.apache.axis2.jaxws.core.controller.impl.AxisInvocationController.execute(AxisInvocationController.java:559)
at org.apache.axis2.jaxws.core.controller.impl.AxisInvocationController.doInvoke(AxisInvocationController.java:118)
at org.apache.axis2.jaxws.core.controller.impl.InvocationControllerImpl.invoke(InvocationControllerImpl.java:82)
at org.apache.axis2.jaxws.client.proxy.JAXWSProxyHandler.invokeSEIMethod(JAXWSProxyHandler.java:317)
at org.apache.axis2.jaxws.client.proxy.JAXWSProxyHandler.invoke(JAXWSProxyHandler.java:159)
at $Proxy12.getData(Unknown Source)
at TestSoaMetroApplet.TestMethod(TestSoaMetroApplet.java:28)
at TestSoaMetroApplet.init(TestSoaMetroApplet.java:19)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.apache.axis2.AxisFault: Transport error: 405 Error: Method not allowed
at org.apache.axis2.transport.http.HTTPSender.handleResponse(HTTPSender.java:295)
at org.apache.axis2.transport.http.HTTPSender.sendViaPost(HTTPSender.java:190)
at org.apache.axis2.transport.http.HTTPSender.send(HTTPSender.java:75)
at org.apache.axis2.transport.http.CommonsHTTPTransportSender.writeMessageWithCommons(CommonsHTTPTransportSender.java:389)
at org.apache.axis2.transport.http.CommonsHTTPTransportSender.invoke(CommonsHTTPTransportSender.java:222)
at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:435)
at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:402)
at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:229)
at org.apache.axis2.client.OperationClient.execute(OperationClient.java:165)
at org.apache.axis2.jaxws.core.controller.impl.AxisInvocationController.execute(AxisInvocationController.java:554)
... 9 more
The exception is obviously caused by an HTTP 405 error, so it is the server, which decides that the client is not allowed to invoke the method. If it is an applet or a standalone Java application should not really matter. Is the applet and the standalone application perhaps accessing the server from different IPs and the server is configured to allow access from the IP used by the standalone app, but denying access from the IP used by the applet?

Categories