ConfigurationBuilder cb = new ConfigurationBuilder();
//... set your keys
String queryString = "Trump";
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
result = twitter.search(query);
List<Status> tweets = result.getTweets();
String json = TwitterObjectFactory.getRawJSON(s);
and I get :
java.lang.IllegalStateException: Apparently jsonStoreEnabled is not
set to true.
Question 1:
I looked through the source code and looks like jsonStoreEnabled is set on ThreadLocal by a call to TwitterObjectFactory.registerJSONObject . I don't think search does that . Does it mean its impossible to get raw json when calling search API ?
Question2:
How do I intercept a call made inside the API to TwitterBaseImpl.httpResponseReceived to get the value of the field before its obscured by layers of the framework ? preferably without AspectJ
Seelenvirtuose is correct , I forgot to do cb.setJSONStoreEnabled(true)
Related
I'm trying to get user details with microsoft-graph
I'm looking for a custom extension element in my response, such as "extension_3a4189d71ad149c6ab5e65ac45bd6add_MyAttribute1"
when I retrieve the response with String, I can see all the elements.
final ResponseEntity<String> response = restTemplate.exchange("http://graph.windows.net/tenant.com/me?api-version=1.6, HttpMethod.GET, new HttpEntity(headers),String.class);
But when I retrieve the response with com.microsoft.graph.models.extensions.User I can't see the extention anymore.
final ResponseEntity<User> response = restTemplate.exchange("http://graph.windows.net/tenant.com/me?api-version=1.6, HttpMethod.GET, new HttpEntity(headers),User.class);
How can I retrieve the custom extension in more elegant way than getting in String object and look for elements one by one?
because the extension attributes are specific to your tenant, that means its non standard, no out of the box "object class" in the sdk would contain it since it has the app id appended to it. extension_appid_attribname.
so you would have to handle it yourself. you can try to extend the user class and add a method to read or deserialize/map the json return from the graph api similar to what Hury suggested, or something to that effect. there won't likely be an out of the box solution for this.
there are also json libraries out there that may help you deserialize to a dynamic object of some sort, if you really didn't want to map the object manually.
Update:
I dug into this a bit further. I don't think its in extensions.extension however, I did find that in the java sdk you can access it . Here's the documentation: https://github.com/microsoftgraph/msgraph-sdk-java/wiki/Working-with-Open-Types
You would do something like
String ext =
user
.additionalDataManager()
.get("extension_2lkj3l12jl3j2kj3_yourproperty")
.getAsString();
Give that a try
Hopefully that helps.
Please use api
https://graph.microsoft.com/beta/users instead of https://graph.microsoft.com/v1.0/users api https://graph.microsoft.com/beta/users will return all data with custom data of users.
Elasticsearch Java High Level REST Client's GET API provides a way to control which fields of the _source are fetched.
val request = GetRequest(index)
.id(id)
.fetchSourceContext(FetchSourceContext(true, includedFields, excludedFields))
elasticClient.get(request, RequestOptions.DEFAULT)
How can I achieve this with the Search APIs?
For example for the following search request:
val source = SearchSourceBuilder()
source.query(QueryBuilders.matchAllQuery())
val request = SearchRequest(index)
.source(source)
elasticClient.search(request, RequestOptions.DEFAULT)
Please refer this from official ES doc,
This method also accepts an array of one or more wildcard patterns to control which fields get included or excluded in a more fine-grained way:
code block
String[] includeFields = new String[] {"title", "innerObject.*"};
String[] excludeFields = new String[] {"user"};
sourceBuilder.fetchSource(includeFields, excludeFields);
Simlar to get API which you already mentioned, you can provide an array of
includeFields and excludeFields to control fetching of the fields from _source fields.
I am using rest high level client elastic search in my JAVA application. Document can be found here.
In my application at startup I am deleting index named "posts" where Elasticsearch datas are stored and creating again Index "posts" following this link
CreateIndexRequest request = new CreateIndexRequest("posts");
But, Inside index I need to create one type named "doc". Which is not mentioned in the website.
Temporary fix is when I am posting some data following this link it is creating type
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("user", "kimchy");
jsonMap.put("postDate", new Date());
jsonMap.put("message", "trying out Elasticsearch");
IndexRequest indexRequest = new IndexRequest("posts", "doc", "1")
.source(jsonMap);
But, in this process when I am posting only then I can able to create type "doc". If I am not posting and trying to hit controller which calls data frmo index "posts" and type "doc". It gives error as "doc" type is not there.
Anyone hava any idea how to create type using rest high level client ES in java
By type you mean document type?
What about the second section Index Mappings in the link you provided?
Does this not work for you?
I needed to set type to "_doc" to make it working with ES7.6
If you know how to insert documents through API then this way will much more easy for you to do anything similar API (DELETE,POST,PUT...)
First, you will need RestHighLevelClient and all you have to do
String index = "/indexName/_doc"; <- Your path or type here
Request request = new Request("POST", index); <- Your method
request.setJsonEntity(
"{ \"message\": \" example add insert\" }" <- Your request body
);
client.getLowLevelClient().performRequest(request);
This will execute like how API does.
I'm trying to save the tweets I got as JSON objects and however still unable to get the JSON object out of Status objects.
I have checked JSONStoreEnabled() and it's set to true.
API says,
Note that raw JSON forms can be retrieved only from the same thread
invoked the last method call and will become inaccessible once another
method call.
I have my everything inside the main() method, so I guess this shouldn't be an issue.
String tweet = TwitterObjectFactory.getRawJSON( status );
I have checked that the status object contains all the information and it's just that getRawJSON returns null!.
Really appreciate if someone can tell me a fix for this.
I am using this approach in my own app. You can try this while configuring Twitter4j.
Using com.google.gson.Gson;
public Gson gson = new Gson();
ConfigurationBuilder config = new ConfigurationBuilder();
config.setJSONStoreEnabled(true);
config.setOAuthConsumerKey(Keys.TWITTER_KEY);
config.setOAuthConsumerSecret(Keys.TWITTER_SECRET);
config.setOAuthAccessToken(currentSession.getAuthToken().token);
config.setOAuthAccessTokenSecret(currentSession.getAuthToken().secret);
Configuration cf = config.build();
// For Twitter4j
enter code here
String statusJson = TwitterObjectFactory.getRawJSON(status); // status to json
This is it.
Gson gson = new Gson();
String json = gson.toJson( status );
System.out.println( json );
new twitter4j.JSONObject(status)
it is works for me
I want to send few dictionaries from django to android through HTTP after getting query in HTTP get. How can I do this and what formatting should I use?
I currently respond using HttpResponse. Name of keys are constant.
Read about serializing objects in django.
You can choose between xml, json or yaml. It's pointless to add documentation here. Goto the link.
EDIT: Django's doc is really nice. Example shouldn't really be needed. But, still, an example from one of my projects [Line 492-507 from views.py].
def pendingOrders(request):
userprof = UserProfile.objects.get(user= request.user)
if userprof.is_student:
student_account = request.user
dish = Dishes.objects.all()
#Getting all pending orders
order_all_pending = Orders.objects.filter(student_id = student_account,delivered = False)
pending_orders = Orders.objects.filter(~Q(status = 2),delivered = False)
for order in order_all_pending:
#Hack to change QuerySet to pass as JSON
order.quantity = pending_orders.filter(id__lt = order.id,counterid= order.counterid).count() + 1
#Returning JSON response to the objects obtained in above statement
return HttpResponse(serializers.serialize('json',order_all_pending,use_natural_keys=True),mimetype='application/json')
else:
return HttpResponse("Something went wrong")
https://stackoverflow.com/a/2845612/931277 Has an example of parsing json from an HttpResponse in Android.
You should use JSON. Django even makes it easy for you.