How to execute an Sparql query in android using androjena 0.5












-1















I'm trying to execute a simple Sparql query from my android device. The online endpoint that I am using is https://dbpedia.org/sparql with URI http://dbpedia.org and the query:




"select distinct ?Concept where { a ?Concept} LIMIT 100".




I successfully imported to my Android project the libraries of androjena 0.5 and arqoid 0.5. Studing the code from this helpful tutorial I changed some lines and I ended up with the following code:






import java.util.List;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.Syntax;

public class SparqlExamples {
public String queryRemoteSparqlEndpoint() {

String queryString = "select distinct ?Concept where { a ?Concept} LIMIT 100";
String sparqlEndpointUri = "https://dbpedia.org";
Query query = QueryFactory.create(queryString, Syntax.syntaxARQ);
query.setLimit(10);
query.setOffset(1);
QueryExecution qe = QueryExecutionFactory.sparqlService(sparqlEndpointUri, query);
ResultSet resultSet = qe.execSelect();
StringBuffer results = new StringBuffer();
List<String> columnNames = resultSet.getResultVars();
while (resultSet.hasNext()) {
QuerySolution solution = resultSet.next();
for (String var : columnNames) {
results.append(var + ": ");
if (solution.get(var) == null) {
results.append("{null}");
} else if (solution.get(var).isLiteral()) {
results.append(solution.getLiteral(var).toString());
} else {
results.append(solution.getResource(var).getURI());
}
results.append('n');
}
results.append("-----------------n");
}
qe.close();
return results.toString();
}
}





and the MainActivity:






import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

String results = new SparqlExamples().queryRemoteSparqlEndpoint();
Log.d("results","results"+String.valueOf(results));
}
}





I will appreciate any idea that could help in this issue.










share|improve this question




















  • 1





    What's the specific problem

    – fedache
    Nov 20 '18 at 21:31











  • Next time, provide the error message you receive... stackoverflow.com/help/how-to-ask

    – TallTed
    Nov 20 '18 at 23:05











  • The specific problem is that I do not receive any response from server and the app crashes after installation. LogCat shows that the problem begins in this command: ResultSet resultSet = qe.execSelect();

    – A Damian
    Nov 21 '18 at 9:20













  • Please edit your question to include the "specific problem" details. Note that the sparqlEndpointUri for DBpedia is https://dbpedia.org/sparql (not https://dbpedia.org), and this must be reflected in your code.

    – TallTed
    Nov 21 '18 at 14:31











  • If there's a crash, there's usually a crash stacktrace you should include here. If I'd have to guess, it's NetworkOnMainThreadException. You need to offload network calls to a background thread.

    – laalto
    Nov 21 '18 at 14:32
















-1















I'm trying to execute a simple Sparql query from my android device. The online endpoint that I am using is https://dbpedia.org/sparql with URI http://dbpedia.org and the query:




"select distinct ?Concept where { a ?Concept} LIMIT 100".




I successfully imported to my Android project the libraries of androjena 0.5 and arqoid 0.5. Studing the code from this helpful tutorial I changed some lines and I ended up with the following code:






import java.util.List;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.Syntax;

public class SparqlExamples {
public String queryRemoteSparqlEndpoint() {

String queryString = "select distinct ?Concept where { a ?Concept} LIMIT 100";
String sparqlEndpointUri = "https://dbpedia.org";
Query query = QueryFactory.create(queryString, Syntax.syntaxARQ);
query.setLimit(10);
query.setOffset(1);
QueryExecution qe = QueryExecutionFactory.sparqlService(sparqlEndpointUri, query);
ResultSet resultSet = qe.execSelect();
StringBuffer results = new StringBuffer();
List<String> columnNames = resultSet.getResultVars();
while (resultSet.hasNext()) {
QuerySolution solution = resultSet.next();
for (String var : columnNames) {
results.append(var + ": ");
if (solution.get(var) == null) {
results.append("{null}");
} else if (solution.get(var).isLiteral()) {
results.append(solution.getLiteral(var).toString());
} else {
results.append(solution.getResource(var).getURI());
}
results.append('n');
}
results.append("-----------------n");
}
qe.close();
return results.toString();
}
}





and the MainActivity:






import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

String results = new SparqlExamples().queryRemoteSparqlEndpoint();
Log.d("results","results"+String.valueOf(results));
}
}





I will appreciate any idea that could help in this issue.










share|improve this question




















  • 1





    What's the specific problem

    – fedache
    Nov 20 '18 at 21:31











  • Next time, provide the error message you receive... stackoverflow.com/help/how-to-ask

    – TallTed
    Nov 20 '18 at 23:05











  • The specific problem is that I do not receive any response from server and the app crashes after installation. LogCat shows that the problem begins in this command: ResultSet resultSet = qe.execSelect();

    – A Damian
    Nov 21 '18 at 9:20













  • Please edit your question to include the "specific problem" details. Note that the sparqlEndpointUri for DBpedia is https://dbpedia.org/sparql (not https://dbpedia.org), and this must be reflected in your code.

    – TallTed
    Nov 21 '18 at 14:31











  • If there's a crash, there's usually a crash stacktrace you should include here. If I'd have to guess, it's NetworkOnMainThreadException. You need to offload network calls to a background thread.

    – laalto
    Nov 21 '18 at 14:32














-1












-1








-1








I'm trying to execute a simple Sparql query from my android device. The online endpoint that I am using is https://dbpedia.org/sparql with URI http://dbpedia.org and the query:




"select distinct ?Concept where { a ?Concept} LIMIT 100".




I successfully imported to my Android project the libraries of androjena 0.5 and arqoid 0.5. Studing the code from this helpful tutorial I changed some lines and I ended up with the following code:






import java.util.List;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.Syntax;

public class SparqlExamples {
public String queryRemoteSparqlEndpoint() {

String queryString = "select distinct ?Concept where { a ?Concept} LIMIT 100";
String sparqlEndpointUri = "https://dbpedia.org";
Query query = QueryFactory.create(queryString, Syntax.syntaxARQ);
query.setLimit(10);
query.setOffset(1);
QueryExecution qe = QueryExecutionFactory.sparqlService(sparqlEndpointUri, query);
ResultSet resultSet = qe.execSelect();
StringBuffer results = new StringBuffer();
List<String> columnNames = resultSet.getResultVars();
while (resultSet.hasNext()) {
QuerySolution solution = resultSet.next();
for (String var : columnNames) {
results.append(var + ": ");
if (solution.get(var) == null) {
results.append("{null}");
} else if (solution.get(var).isLiteral()) {
results.append(solution.getLiteral(var).toString());
} else {
results.append(solution.getResource(var).getURI());
}
results.append('n');
}
results.append("-----------------n");
}
qe.close();
return results.toString();
}
}





and the MainActivity:






import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

String results = new SparqlExamples().queryRemoteSparqlEndpoint();
Log.d("results","results"+String.valueOf(results));
}
}





I will appreciate any idea that could help in this issue.










share|improve this question
















I'm trying to execute a simple Sparql query from my android device. The online endpoint that I am using is https://dbpedia.org/sparql with URI http://dbpedia.org and the query:




"select distinct ?Concept where { a ?Concept} LIMIT 100".




I successfully imported to my Android project the libraries of androjena 0.5 and arqoid 0.5. Studing the code from this helpful tutorial I changed some lines and I ended up with the following code:






import java.util.List;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.Syntax;

public class SparqlExamples {
public String queryRemoteSparqlEndpoint() {

String queryString = "select distinct ?Concept where { a ?Concept} LIMIT 100";
String sparqlEndpointUri = "https://dbpedia.org";
Query query = QueryFactory.create(queryString, Syntax.syntaxARQ);
query.setLimit(10);
query.setOffset(1);
QueryExecution qe = QueryExecutionFactory.sparqlService(sparqlEndpointUri, query);
ResultSet resultSet = qe.execSelect();
StringBuffer results = new StringBuffer();
List<String> columnNames = resultSet.getResultVars();
while (resultSet.hasNext()) {
QuerySolution solution = resultSet.next();
for (String var : columnNames) {
results.append(var + ": ");
if (solution.get(var) == null) {
results.append("{null}");
} else if (solution.get(var).isLiteral()) {
results.append(solution.getLiteral(var).toString());
} else {
results.append(solution.getResource(var).getURI());
}
results.append('n');
}
results.append("-----------------n");
}
qe.close();
return results.toString();
}
}





and the MainActivity:






import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

String results = new SparqlExamples().queryRemoteSparqlEndpoint();
Log.d("results","results"+String.valueOf(results));
}
}





I will appreciate any idea that could help in this issue.






import java.util.List;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.Syntax;

public class SparqlExamples {
public String queryRemoteSparqlEndpoint() {

String queryString = "select distinct ?Concept where { a ?Concept} LIMIT 100";
String sparqlEndpointUri = "https://dbpedia.org";
Query query = QueryFactory.create(queryString, Syntax.syntaxARQ);
query.setLimit(10);
query.setOffset(1);
QueryExecution qe = QueryExecutionFactory.sparqlService(sparqlEndpointUri, query);
ResultSet resultSet = qe.execSelect();
StringBuffer results = new StringBuffer();
List<String> columnNames = resultSet.getResultVars();
while (resultSet.hasNext()) {
QuerySolution solution = resultSet.next();
for (String var : columnNames) {
results.append(var + ": ");
if (solution.get(var) == null) {
results.append("{null}");
} else if (solution.get(var).isLiteral()) {
results.append(solution.getLiteral(var).toString());
} else {
results.append(solution.getResource(var).getURI());
}
results.append('n');
}
results.append("-----------------n");
}
qe.close();
return results.toString();
}
}





import java.util.List;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.Syntax;

public class SparqlExamples {
public String queryRemoteSparqlEndpoint() {

String queryString = "select distinct ?Concept where { a ?Concept} LIMIT 100";
String sparqlEndpointUri = "https://dbpedia.org";
Query query = QueryFactory.create(queryString, Syntax.syntaxARQ);
query.setLimit(10);
query.setOffset(1);
QueryExecution qe = QueryExecutionFactory.sparqlService(sparqlEndpointUri, query);
ResultSet resultSet = qe.execSelect();
StringBuffer results = new StringBuffer();
List<String> columnNames = resultSet.getResultVars();
while (resultSet.hasNext()) {
QuerySolution solution = resultSet.next();
for (String var : columnNames) {
results.append(var + ": ");
if (solution.get(var) == null) {
results.append("{null}");
} else if (solution.get(var).isLiteral()) {
results.append(solution.getLiteral(var).toString());
} else {
results.append(solution.getResource(var).getURI());
}
results.append('n');
}
results.append("-----------------n");
}
qe.close();
return results.toString();
}
}





import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

String results = new SparqlExamples().queryRemoteSparqlEndpoint();
Log.d("results","results"+String.valueOf(results));
}
}





import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

String results = new SparqlExamples().queryRemoteSparqlEndpoint();
Log.d("results","results"+String.valueOf(results));
}
}






android sparql jena






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 14:30









TallTed

6,14321527




6,14321527










asked Nov 20 '18 at 21:15









A DamianA Damian

12




12








  • 1





    What's the specific problem

    – fedache
    Nov 20 '18 at 21:31











  • Next time, provide the error message you receive... stackoverflow.com/help/how-to-ask

    – TallTed
    Nov 20 '18 at 23:05











  • The specific problem is that I do not receive any response from server and the app crashes after installation. LogCat shows that the problem begins in this command: ResultSet resultSet = qe.execSelect();

    – A Damian
    Nov 21 '18 at 9:20













  • Please edit your question to include the "specific problem" details. Note that the sparqlEndpointUri for DBpedia is https://dbpedia.org/sparql (not https://dbpedia.org), and this must be reflected in your code.

    – TallTed
    Nov 21 '18 at 14:31











  • If there's a crash, there's usually a crash stacktrace you should include here. If I'd have to guess, it's NetworkOnMainThreadException. You need to offload network calls to a background thread.

    – laalto
    Nov 21 '18 at 14:32














  • 1





    What's the specific problem

    – fedache
    Nov 20 '18 at 21:31











  • Next time, provide the error message you receive... stackoverflow.com/help/how-to-ask

    – TallTed
    Nov 20 '18 at 23:05











  • The specific problem is that I do not receive any response from server and the app crashes after installation. LogCat shows that the problem begins in this command: ResultSet resultSet = qe.execSelect();

    – A Damian
    Nov 21 '18 at 9:20













  • Please edit your question to include the "specific problem" details. Note that the sparqlEndpointUri for DBpedia is https://dbpedia.org/sparql (not https://dbpedia.org), and this must be reflected in your code.

    – TallTed
    Nov 21 '18 at 14:31











  • If there's a crash, there's usually a crash stacktrace you should include here. If I'd have to guess, it's NetworkOnMainThreadException. You need to offload network calls to a background thread.

    – laalto
    Nov 21 '18 at 14:32








1




1





What's the specific problem

– fedache
Nov 20 '18 at 21:31





What's the specific problem

– fedache
Nov 20 '18 at 21:31













Next time, provide the error message you receive... stackoverflow.com/help/how-to-ask

– TallTed
Nov 20 '18 at 23:05





Next time, provide the error message you receive... stackoverflow.com/help/how-to-ask

– TallTed
Nov 20 '18 at 23:05













The specific problem is that I do not receive any response from server and the app crashes after installation. LogCat shows that the problem begins in this command: ResultSet resultSet = qe.execSelect();

– A Damian
Nov 21 '18 at 9:20







The specific problem is that I do not receive any response from server and the app crashes after installation. LogCat shows that the problem begins in this command: ResultSet resultSet = qe.execSelect();

– A Damian
Nov 21 '18 at 9:20















Please edit your question to include the "specific problem" details. Note that the sparqlEndpointUri for DBpedia is https://dbpedia.org/sparql (not https://dbpedia.org), and this must be reflected in your code.

– TallTed
Nov 21 '18 at 14:31





Please edit your question to include the "specific problem" details. Note that the sparqlEndpointUri for DBpedia is https://dbpedia.org/sparql (not https://dbpedia.org), and this must be reflected in your code.

– TallTed
Nov 21 '18 at 14:31













If there's a crash, there's usually a crash stacktrace you should include here. If I'd have to guess, it's NetworkOnMainThreadException. You need to offload network calls to a background thread.

– laalto
Nov 21 '18 at 14:32





If there's a crash, there's usually a crash stacktrace you should include here. If I'd have to guess, it's NetworkOnMainThreadException. You need to offload network calls to a background thread.

– laalto
Nov 21 '18 at 14:32












1 Answer
1






active

oldest

votes


















0














This --



    String sparqlEndpointUri = "https://dbpedia.org";


-- should be this --



    String sparqlEndpointUri = "https://dbpedia.org/sparql";





share|improve this answer
























  • I tried this but still is not working. However, thanks for the interest.

    – A Damian
    Nov 21 '18 at 9:29






  • 1





    @ADamian "not working" is not helpful... what does not work? errors, empty result, etc?

    – AKSW
    Nov 22 '18 at 4:03













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401648%2fhow-to-execute-an-sparql-query-in-android-using-androjena-0-5%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














This --



    String sparqlEndpointUri = "https://dbpedia.org";


-- should be this --



    String sparqlEndpointUri = "https://dbpedia.org/sparql";





share|improve this answer
























  • I tried this but still is not working. However, thanks for the interest.

    – A Damian
    Nov 21 '18 at 9:29






  • 1





    @ADamian "not working" is not helpful... what does not work? errors, empty result, etc?

    – AKSW
    Nov 22 '18 at 4:03


















0














This --



    String sparqlEndpointUri = "https://dbpedia.org";


-- should be this --



    String sparqlEndpointUri = "https://dbpedia.org/sparql";





share|improve this answer
























  • I tried this but still is not working. However, thanks for the interest.

    – A Damian
    Nov 21 '18 at 9:29






  • 1





    @ADamian "not working" is not helpful... what does not work? errors, empty result, etc?

    – AKSW
    Nov 22 '18 at 4:03
















0












0








0







This --



    String sparqlEndpointUri = "https://dbpedia.org";


-- should be this --



    String sparqlEndpointUri = "https://dbpedia.org/sparql";





share|improve this answer













This --



    String sparqlEndpointUri = "https://dbpedia.org";


-- should be this --



    String sparqlEndpointUri = "https://dbpedia.org/sparql";






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 23:04









TallTedTallTed

6,14321527




6,14321527













  • I tried this but still is not working. However, thanks for the interest.

    – A Damian
    Nov 21 '18 at 9:29






  • 1





    @ADamian "not working" is not helpful... what does not work? errors, empty result, etc?

    – AKSW
    Nov 22 '18 at 4:03





















  • I tried this but still is not working. However, thanks for the interest.

    – A Damian
    Nov 21 '18 at 9:29






  • 1





    @ADamian "not working" is not helpful... what does not work? errors, empty result, etc?

    – AKSW
    Nov 22 '18 at 4:03



















I tried this but still is not working. However, thanks for the interest.

– A Damian
Nov 21 '18 at 9:29





I tried this but still is not working. However, thanks for the interest.

– A Damian
Nov 21 '18 at 9:29




1




1





@ADamian "not working" is not helpful... what does not work? errors, empty result, etc?

– AKSW
Nov 22 '18 at 4:03







@ADamian "not working" is not helpful... what does not work? errors, empty result, etc?

– AKSW
Nov 22 '18 at 4:03




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401648%2fhow-to-execute-an-sparql-query-in-android-using-androjena-0-5%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?