Change in the response format of solrj - java

Previously I was using the solrj 4.9.1 for connecting with solrserver 4.9.1.
Now I have upgraded it to solrj 5.4.1 to connect with solrserver 5.4.1. But the problem is that previously the code,
QueryResponse res = null;
try {
res=solrServer.query(query);
} catch (SolrServerException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
used to work properly. But now in solrj 5.4.1 it is throwing an IO Exception for processing the same data. When I have changed the code as,
QueryResponse res = null;
try {
solrServer.query(query);
} catch (SolrServerException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
it is passing through. Although I am not getting any result set. But there is no error during the query. So what I think is that there is some problem or change in the QueryResponse constructor. But I have not found any change in the solrj documentation.
Both in 5.4.1 as well as in 4.9.1
https://lucene.apache.org/solr/5_4_1/solr-solrj/org/apache/solr/client/solrj/response/QueryResponse.html
https://lucene.apache.org/solr/4_9_1/solr-solrj/org/apache/solr/client/solrj/response/QueryResponse.html
only that SolrServer is changed to SolrClient. Is there really any change? or I am doing it wrongly?

Related

using WebClient and getpage

I'm trying to get information from a Html page using WebClient but I don't manage to get the correct page. It's not waiting for the query to be finished.
Anyone has an idea? It must be linked to the website I'm trying to read. For others website my code is working
Here an extract of the code I use to get the Html page
public void initWebClient() {
WebClient _webClient = new WebClient(BrowserVersion.FIREFOX_10);
_webClient.getOptions().setJavaScriptEnabled(true);
_webClient.getOptions().setThrowExceptionOnScriptError(false);
_webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
_webClient.waitForBackgroundJavaScript(10000);
_webClient.waitForBackgroundJavaScriptStartingBefore(10000);
_webClient.setCssEnabled(false);
try {
HtmlPage _pageRecherche = (HtmlPage) _webClient.getPage("https://www.blablacar.fr/trajets/paris/lyon/#?fn=PAris&fc=48.856614%7C2.352222&fcc=FR&fp=0&tn=LYON&tc=45.764043%7C4.835659&tcc=FR&tp=0&db=25%2F05%2F2017&sort=trip_date&order=asc&limit=10&page=1");
} catch (FailingHttpStatusCodeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Thanks
Joe
Use this code block inside the try-catch block not outside. If this fails check with another OS.
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_10);
webClient.getOptions().setJavaScriptEnabled(false);
webClient.getOptions().setUseInsecureSSL(true);
HtmlPage page = (HtmlPage) webClient.getPage("https://stackoverflow.com/users/login");
System.out.println("Yeyy! :) Loaded Page!");

How to catch an Exception and a SocketTimeOut Exception in one try/catch block

I have implemented a try catch block for URLconnection as well as Parser class as follow.
try {
Url uri = new Url(urlString);
Parser parse = new Parser(uri);
} catch (Exception e)
{
//ignore some other exceptions
}
catch (SocketTimeOutException e)
{
//I want to catch this exception and do some thing or restart
//if it's a timeout issue.
//I am using a proxy for the network connection at JVM setting
//using setProperty
}
So , my question is how to act accordingly based on the SocketTimeOutException case , and for other Exception ignore .
Thanks ,
As java specification says (http://docs.oracle.com/javase/specs/jls/se7/html/jls-11.html#jls-11.2.3 and http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.20), first matched, first executed.
Simply invert your catch clause :
try {
Url uri = new Url(urlString);
Parser parse = new Parser(uri);
} catch (SocketTimeOutException e) {
//I want to cache this ecption and do some thing or restart based
//if its timeout issue
//am using proxy for the network connection at JVM setting
//using setProperty
} catch (Exception e) {
//ingnore some other excpetions
}
Put more specific exception types above more general types, so put the SocketTimeoutException catch clause above Exception
Catch the SocketTimeOutException first:
try {
// do stuff
} catch (SocketTimeOutException e) {
// restart or do whatever you need to do
} catch (Exception e) {
// do something else
}
How to catch an Exception and a SocketTimeOut Exception in one try/catch block?? If you wan't to have only one catch block then you can do like this
try {
URI uri = new URI(urlString);
Parser parse = new Parser(uri);
} catch(Exception e) {
if (e instanceof SocketTimeoutException) {
// do something
}
}

Getting a 405 response code with HttpURLConnection

I am getting a HTTP response code 405 when I use HttpURLConnection to make a DELETE request:
public void makeDeleteRequest(String objtype,String objkey)
{
URL url=null;
String uri="http://localhost:8180/GoogleMapsLoadingTest/rest/GoogleMapsErp/";
HttpURLConnection conn=null;
try {
url=new URL(uri);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn=(HttpURLConnection) url.openConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn.setDoInput(true);
conn.setDoOutput(true);
try {
System.out.println(conn.getResponseCode());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
conn.setRequestMethod("DELETE");
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
How can I make this request?
The 405 status code means that your method (DELETE) is not allowed for the resource you specified; in this case, what looks like an entire REST endpoint directory. You should use DELETE on the specific item you want deleted; perhaps you forgot to actually use the method parameters when constructing the URL?
It would appear to be a problem with your server. I'm guessing nginx, but let me know if not.
Take a look here to see how to compile ngninx with HttpDavModule. There are other servers that have issues with this, but nginx doesn't have it by default.
If you're running Apache, check your module configurations to see if you're disallowing them or not. Here's a post about a previous solution. Unfortunately, this problem is typically specific to modules that have been installed. On a vanilla installation, however, you can often just allow for DELETE (see your config file, as well as the OP of that link)

Self-signed certificate

I use a hosting with a self-signed certificate. So I downloaded the certificate from my domain https://www.marpel.cz/ and created .bks file using http://portecle.sourceforge.net/.
I need to establish the https connection and retrieve data from my webservice. I use ksoap2 library. I have copied and used a class ConnectionWithSelfSignedCertificate stated in ksoap2 wiki.
This is the way I create a keyStore
MainActivity.java
// Get an instance of the Bouncy Castle KeyStore format
try {
this.keyStore = KeyStore.getInstance("BKS");
} catch (KeyStoreException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Get the raw resource, which contains the keystore with
// your trusted certificates (root and any intermediate certs)
InputStream in = this.getApplicationContext().getResources().openRawResource(R.raw.myCer);
try {
// Initialize the keystore with the provided trusted certificates
// Also provide the password of the keystore
this.keyStore.load(in, "myPass".toCharArray());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
this.sslSocketFactory = new ConnectionWithSelfSignedCertificate(this.keyStore).getSSLSocketFactory();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
And this is a code from AsyncTask
background task
final HttpsTransportSE transportSE = new HttpsTransportSE(URL, PORT, SERVICE, TIMEOUT);
try {
((HttpsServiceConnectionSE) transportSE.getServiceConnection()).setSSLSocketFactory(this.sslSocketFactory);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
If I call transportSE.call(SOAP_ACTION, envelope); I get IOException, Hostname 'www.marpel.cz' was not verified. What do I do wrong?
I have an ICS 4.1.2 device.
Firstly, do you use self signed certificate?
If yes, then follow this link: android-webservices-via-ksoap2-https
You need extra classes for creating https connection and accepting of the certificate. When everything is prepared, you can call your transportSE.
The code in my first post works fine. I have found out that the self-signed certificate was issued for different domain. I fixed the certificate and everything works fine.
The fixed certificate runs here https://www.marpel.cz:445/
Thank you, Martin.

error: URL can not open stream

When I use openStream() to parse XML from a web service, I get the error:
Can not open stream
I've checked the URL and it's still working.
URL url = new URL("http://uitbookshop.php0h.com/PHPService/findbyname.php?name=thu");
Log.d("search", "getXML::url "+url.toString());
InputStream iS;
try {
iS = url.openStream();
doc=db.parse(new InputSource(new InputStreamReader(iS, Charset.forName("utf-8"))));
} catch (IOException e) {
Log.e("search", "Can not open stream");
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Have you given appropriate Permissions?
make sure you have following permission added to the manifest
android.permission.INTERNET

Categories

Resources