PostForEntity data is not saving in database - java

public UaaGroup createGroup() {
String requestUrl = appConfig.getUaa().getBase_url() + "/Groups";
LOGGER.info("requestUrl : {}", requestUrl);
UaaGroup uaaGroup = new UaaGroup();
uaaGroup.setDescription("description");
uaaGroup.setDisplayName(UUID.randomUUID().toString());
LOGGER.info("DisplayName before rest call : {}", uaaGroup.getDisplayName());
try {
ResponseEntity<UaaGroup> responseEntity = restTemplate.postForEntity(requestUrl, uaaGroup, UaaGroup.class,
"");
uaaGroup = responseEntity.getBody();
LOGGER.info("UaaGroupServiceImpl.createGroup: uaaGroup={}", responseEntity.getBody().toString());
return uaaGroup;
} catch (Exception e) {
LOGGER.error("Create UAA Group failed: {}", e);
throw e;
}
}
public UaaGroup updateGroup(String groupId, GroupRequest groupRequest) {
String requestUrl = appConfig.getUaa().getBase_url() + "/Groups/{groupId}";
UaaGroup uaaGroup = new UaaGroup();
if (!Strings.isNullOrEmpty(groupId)) {
String displayName = "eid-" + groupRequest.getEnterpriseId() + '-' + "gid-" + groupId + '-'
+ groupRequest.getRole();
String description = groupRequest.getEnterpriseName() + ":" + groupRequest.getName();
uaaGroup.setDisplayName(displayName);
uaaGroup.setDescription(description);
try {
HttpEntity<UaaGroup> entity = new HttpEntity<UaaGroup>(uaaGroup);
ResponseEntity<UaaGroup> responseEntity = restTemplate.exchange(requestUrl, HttpMethod.PUT, entity,
UaaGroup.class, groupId);
uaaGroup = responseEntity.getBody();
LOGGER.info("Updated Group", responseEntity.getBody().toString());
return uaaGroup;
} catch (Exception e) {
LOGGER.info("Failed to update the Group: {}", e.getMessage());
}
}
return uaaGroup;
}
#Override
public UaaGroup handleGroup(GroupRequest request) {
UaaGroup uaaGroup = this.createGroup();
LOGGER.info("handleGroup() createdGroup: {}", uaaGroup);
UaaGroupList uaaGroupList = uaaService.listUaaGroups(); /** newly created group is not displaying here
String groupId = "";
if (uaaGroup != null) {
for (UaaGroupList.Resources resources : uaaGroupList.getResources()) {
if (uaaGroup.getDisplayName().equals(resources.getDisplayName())) {
groupId = resources.getId();
LOGGER.info("groupId: {}", groupId);
}
}
}
// if (Strings.isNullOrEmpty(groupId)) {
// UaaGroup uaagroup = createGroup();
// uaaGroupList = uaaService.listUaaGroups();
// if (uaaGroupList != null) {
// for (UaaGroupList.Resources resources : uaaGroupList.getResources()) {
// if (uaagroup.getDisplayName().equals(resources.getDisplayName())) {
// groupId = resources.getId();
// LOGGER.info("Uaa User Group Id found: {}", groupId);
// }
// }
// if (Strings.isNullOrEmpty(groupId)) {
// // this should never happen...
// LOGGER.error("Failed to create UAA Group : {}");
// }
// }
// }
// }
if (!Strings.isNullOrEmpty(groupId)) {
LOGGER.info("grupId:{}", groupId);
uaaGroup = updateGroup(groupId, request);
LOGGER.info("upfatedGroup : {}", uaaGroup);
return uaaGroup;
}
return uaaGroup; // every time I am getting only createGroup object
}
while creating group first I want to create a group with randomUUID and by using that random UUID i will try to get the groupId. In my case after creating the group .I am not able to see the newly created group in listof groups.
In handleGroup() method every time iam getting created group Object but that created group i snout displaying in list of groups

Related

S3 GetBucketACL() throws exception For Singapore Bucket - The bucket you are attempting to access must be addressed using the specified endpoint

I am creating AWS bucket of 'Singapore' region. After creating I am getting acl of BUCKET, that time it throw exception - "(S3Exception) software.amazon.awssdk.services.s3.model.S3Exception: The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint. (Service: S3, Status Code: 301)"
My code is -
Main Method
{
s3Client = awsJdkClient.createCredentialsWithAWSJdk(sLocationConstaints);
sBucket = awsJdkClient.createAWSBucket(s3Client, sTempBucketName);
AmazonS3Requests.increase_PUT_BUCKET_Requeset();
if (getDetails(sBucket) == true) {
objBucketOperationEvent.addMessageToSatusbar("Bucket " + sBucket + " has been created successfully.", true);
} else {
objBucketOperationEvent.addMessageToSatusbar("Bucket " + sBucket + " has not been created.", true);
return false;
}
}
public GetBucketAclResponse getBucketACL( String bucketName) {
try {
GetBucketAclRequest getBucketAclRequest = GetBucketAclRequest.builder().bucket(bucketName).build();
GetBucketAclResponse bucketAclS3Client = this.s3Client.getBucketAcl(getBucketAclRequest); //Here it throw exception
return bucketAclS3Client;
} catch (S3Exception e) {
throw e;
}
}
private boolean getDetails(String bucketname) {
try {
objBucketExplorer.writeDebug("CreateBucket: getDetails() is invoked");
if (awsJdkClient == null) {
objBucketExplorer.writeDebug("CreateBucket-while getting detail: return unsuccessfully from getDetails() due to objService found null");
return false;
}
if (awsJdkClient.getBucketACL(bucketname) == null) {
AmazonS3Requests.increase_GET_BUCKET_Requeset();
return false;
} else {
AmazonS3Requests.increase_GET_BUCKET_Requeset();
return true;
}
} catch (S3Exception ex) {
String stackNum = Utility.exceptionHandler(ex);
objBucketExplorer.writeDebug("CreateBucket: return unsuccessfully from getDetails() :Error " + ex.getMessage() + " Stacktrace " + stackNum);
return false;
}
}
public S3Client createCredentialsWithAWSJdk(String regionString) {
try {
AwsBasicCredentials awsCreds = AwsBasicCredentials.create(
ACCESS_KEY,
SECRET_KEY);
S3ClientBuilder s3ClientBuilder = S3Client.builder().credentialsProvider(StaticCredentialsProvider.create(awsCreds));
if (regionString != null && !regionString.isEmpty()) {
Region region = Region.of(regionString);
s3ClientBuilder.region(region);
}
S3Client s3Client = s3ClientBuilder.build();
return s3Client;
} catch (S3Exception ex) {
throw ex;
}
}
public String createAWSBucket(S3Client s3Client, String bucketName) {
try {
S3Waiter s3Waiter = s3Client.waiter();
CreateBucketRequest bucketRequest = CreateBucketRequest.builder()
.bucket(bucketName)
.build();
CreateBucketResponse few = s3Client.createBucket(bucketRequest);
HeadBucketRequest bucketRequestWait = HeadBucketRequest.builder()
.bucket(bucketName)
.build();
return bucketRequestWait.bucket();
} catch (S3Exception e) {
if(e.awsErrorDetails().errorCode().equalsIgnoreCase("BucketAlreadyOwnedByYou")){
return bucketName;
}
throw e;
}
}

Calculate delta Offsets Kafka Java

In a spring project i used Kafka and now I want to make a method which takes "TopicName" and "GroupeId" as parameters
and calculate the difference between "Lastoffsets of the topic partitions" and the "offsets consumed by the group"
for the lastOffsets i get it
now i need to get the consumed offsets to calculate the difference
public ResponseEntity<Offsets> deltaoffsets (#RequestParam( name = "groupId") String groupId, #RequestParam( name = "topic") String topic) {
Map<String,Object> properties = (Map) kafkaLocalConsumerConfig.get("kafkaLocalConsumerConfig");
properties.put("group.id", groupId);
properties.put("enable.auto.commit", "true");
List<TopicPartition> partition=new ArrayList<>();
KafkaConsumer<String, RefentialToReload> kafkaLocalConsumer = new KafkaConsumer<>(properties);
Map<String, List<PartitionInfo>> topics = kafkaLocalConsumer.listTopics();
List<PartitionInfo> partitionInfos = topics.get(topic);
if (partitionInfos == null) {
log.warn("Partition information was not found for topic");
}
else {
for (PartitionInfo partitionInfo : partitionInfos) {
TopicPartition topicPartition = new TopicPartition(topic, partitionInfo.partition());
partition.add(topicPartition);
log.info("partition assigned to kafkaLocalConsumer");
}
}
//get lastOffsets of the topicPartition
Map<TopicPartition,Long> OffsetsTopicpartition = kafkaLocalConsumer.endOffsets(kafkaLocalConsumer.assignment());
//here i need to get consumed offsets
}
beginningOffsets() is the first offsets, not the last.
You can use an AdminClient - here is an example that displays the current and end offsets...
#Bean
public ApplicationRunner runner(KafkaAdmin admin, ConsumerFactory<String, String> cf) throws Exception {
return args -> {
try (
AdminClient client = AdminClient.create(admin.getConfig());
Consumer<String, String> consumer = cf.createConsumer("group", "clientId", "");
) {
Collection<ConsumerGroupListing> groups = client.listConsumerGroups()
.all()
.get(10, TimeUnit.SECONDS);
groups.forEach(group -> {
Map<TopicPartition, OffsetAndMetadata> map = null;
try {
map = client.listConsumerGroupOffsets(group.groupId())
.partitionsToOffsetAndMetadata()
.get(10, TimeUnit.SECONDS);
}
catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
catch (ExecutionException e) {
e.printStackTrace();
}
catch (TimeoutException e) {
e.printStackTrace();
}
Map<TopicPartition, Long> endOffsets = consumer.endOffsets(map.keySet());
map.forEach((tp, off) -> {
System.out.println("group: " + group + " tp: " + tp
+ " current offset: " + off.offset()
+ " end offset: " + endOffsets.get(tp));
});
});
}
};
}

Sending push notifications to multiple devices using FCM, SQL, java

I have a work service which sending push notifications to the registration device and it works well, but I have a problem sending push notifications to multiple devices. I'm trying to use loops but it doesn't help. I think that to count the responses maybe give me results. Maybe the problem in the boolean expression, I don't guess. Maybe somebody knows what to do in this situation. Thanks for response and code below:
#RequestMapping(value = "/sendtodeviceid", method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<String> send() throws JSONException, SQLException {
try {
System.out.println("\n" + "send massege to devicesid" + "\n" + "start to get ID" + "\n");
ArrayList <String> deviceList = new ArrayList<>();
Statement statement = jdbcService.getStatement();
String selSql = String.format("SELECT deviceid FROM courier WHERE isActive = true");
ResultSet rs = statement.executeQuery(selSql);
// check equals for more deviceid
while (rs.next()) {
String deviceid = rs.getString("deviceid");
System.out.println("DEVAICE ID which true from sending message " + "\n" + deviceid + "\n");
deviceList.add(deviceid);
if (!deviceid.equals(deviceid)) {
String newdeviceid = deviceid;
deviceList.add(newdeviceid);
}
}
System.out.println(deviceList + "\n");
// find some solution for loop sending message to all device
for (String iddevice: deviceList) {
System.out.println("DEVICE ID: " + iddevice + "\n");
// create jsonObject look like this
// {
// "data":
// {"address":"latitude420&longitude420",
// "click_action":".MessageOrder",
// "order":"#420"},
// "to":
// "d2Hxxa6PNYw",
// "priority":"high"
// },{}
do {
JSONObject body = new JSONObject();
body.put("to", iddevice);
body.put("priority", "high");
// JSONObject notification = new JSONObject();
// notification.put("title", "Wise delivery");
// notification.put("body", "It is personal order!!!");
// notification.put("icon", "main_logo_black");
// notification.put("click_action", ".MessageOrder");
JSONObject data = new JSONObject();
data.put("order", "#421");
data.put("address", "latitude420&longitude421");
data.put("click_action", ".MessageOrder");
// data.put("icon","main_logo_black");
// body.put("notification", notification);
body.put("data", data);
HttpEntity<String> request = new HttpEntity<>(body.toString());
System.out.println("JSON file request" + request);
CompletableFuture<String> pushNotification = androidPushNotificationsService.send(request);
CompletableFuture.allOf(pushNotification).join();
try {
String firebaseResponse = pushNotification.get();
return new ResponseEntity<>(firebaseResponse, HttpStatus.OK);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
} while (iddevice == null);
}
} catch (SQLException e) {
System.out.println("don't selectSQL" + "\n" + "SELECT deviceid FROM courier ");
}
return new ResponseEntity<>("Push Notification ERROR!", HttpStatus.BAD_REQUEST);
}
And terminal shows this:
// start
send message to devicesid
start to get ID
// get device id from database
DEVAICE ID which true from sending message
d2Hxxa6PNYw:
DEVAICE ID which true from sending message
eb0s9KRXac8:
// create the ArrayList
[d2Hxxa6PNYw, eb0s9KRXac8]
// and at this step I have a problem
DEVICE ID: d2Hxxa6PNYw
JSON file request<{"data":{"address":"latitude420&longitude421",
"click_action":".MessageOrder",
"order":"#421"},
"to":"d2Hxxa6PNYw",
"priority":"high"},{}>
// after this must be the next step in loop)
Instead of using a loop, you can subscribe users to a specific topic. Then you can send notifications using Firebase Console or writing server-side logic.
FirebaseMessaging.getInstance().subscribeToTopic("news")
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
String msg = getString(R.string.msg_subscribed);
if (!task.isSuccessful()) {
msg = getString(R.string.msg_subscribe_failed);
}
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
My friend helped me and all working well
#RequestMapping(value = "/sendtodeviceid", method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<String> send() throws Exception {
System.out.println("\n" + "send massege to devicesid" + "\n" + "start to get ID" + "\n");
ArrayList<String> deviceList = new ArrayList<>();
Statement statement = jdbcService.getStatement();
String selSql = String.format("SELECT deviceid FROM courier WHERE isActive = true");
ResultSet rs = statement.executeQuery(selSql);
while (rs.next()) {
String deviceid = rs.getString("deviceid");
System.out.println("DEVAICE ID which true from sending message " + "\n" + deviceid + "\n");
deviceList.add(deviceid);
if (!deviceid.equals(deviceid)) {
String newdeviceid = deviceid;
deviceList.add(newdeviceid);
}
}
System.out.println(deviceList + "\n");
List<CompletableFuture> sentNotifications = new ArrayList<CompletableFuture>();
for (String deviceId : deviceList) {
HttpEntity<String> request = new HttpEntity<>(_buildRequestJsonBody(deviceId).toString());
CompletableFuture<String> pushNotification = androidPushNotificationsService.send(request);
sentNotifications.add(pushNotification);
}
CompletableFuture.allOf(sentNotifications.toArray(new CompletableFuture[sentNotifications.size()])).join();
for (CompletableFuture<String> notification : sentNotifications) {
String firebaseResponse = notification.get();
System.out.println("RESPONSE " + firebaseResponse);
return new ResponseEntity<>(firebaseResponse, HttpStatus.OK);
}
return new ResponseEntity<>("Push Notification ERROR!", HttpStatus.BAD_REQUEST);
}
private JSONObject _buildRequestJsonBody(String deviceId) {
JSONObject body = new JSONObject();
body.put("to", deviceId);
body.put("priority", "high");
JSONObject data = new JSONObject();
data.put("order", "#421");
data.put("address", "latitude420&longitude421");
data.put("click_action", ".MessageOrder");
body.put("data", data);
return body;
}
}

Spring Statemachine Factory -stays in memory

I have used Spring state-machine in quite a complex scenario. I will explain my problem with the simplest part of the SM. Refer below image. This is my main state machine
The state circled in red points to the following sub-machine
So, as you can see, I have 3 actions. sendBasicTemplate, timeoutLogAction and processBasicTemplateReply. I will provide the related code segments and my configuration below.
What I have observed during this process is that the state-machines created by the factory resides in memory always. There's some reference to it which i cannot think of.
Is it that the SM doesn't stop or is there anything I'm doing wrong? Here's my code.
Configuration class
#Configuration #EnableStateMachineFactory public class CambodiaStateMachine extends StateMachineConfigurerAdapter<String, String> {
#Override
public void configure(StateMachineModelConfigurer<String, String> model) throws Exception {
model
.withModel()
.factory(modelFactory());
}
#Override public void configure(StateMachineConfigurationConfigurer<String, String> config) throws Exception {
config
.withConfiguration()
.machineId("cambodia")
.autoStartup(true)
.listener(listener()); }
#Bean
public StateMachineListener<String, String> listener() {
return new StateMachineListenerAdapter<String, String>() {
#Override
public void stateChanged(State<String, String> from, State<String, String> to) {
System.out.println("State change to " + to.getId());
}
};
}
#Bean
public StateMachineModelFactory<String, String> modelFactory() {
return new UmlStateMachineModelFactory("classpath:stm/model.uml");
}
}
Methods : 1. This is how my events are fed to the machine and where new SM instances are made. I take my events from a queue
#RabbitListener(bindings = #QueueBinding(value = #Queue(value = "sims.events.mq", durable = "true"), exchange = #Exchange(type = ExchangeTypes.TOPIC, value = "sims.events.mq.xch", ignoreDeclarationExceptions = "true", durable = "true"), key = "events"))
public void process(GenericMessage<String> message) {
try {
String imei = (String) message.getHeaders().get("imei");
Subscriber subscriber = subscriberService.findSubscriber(imei);
// quickly create 'new' state machine
StateMachine<String, String> stateMachine = factory.getStateMachine();
stateMachine.addStateListener(new CompositeStateMachineListener<String, String>() {
#Override
public void stateContext(StateContext<String, String> arg0) {
String user = (String) arg0.getExtendedState().getVariables().get("imei");
if (user == null) {
return;
}
log.info(arg0.getStage().toString() + "**********" + stateMachine.getState());
try {
redisStateMachinePersister.persist(arg0.getStateMachine(), "testprefixSw:" + user);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
});
// restore from persistent
String user = (String) message.getHeaders().get("imei");
log.info(user);
// attempt restoring only if key is exist
if (redisTemplate.hasKey("testprefixSw:" + user)) {
System.out.println("************************ prefix exists...restoring");
resetStateMachineFromStore(stateMachine, user);
} else {
stateMachine.start();
System.out.println("************************ No prefix");
}
log.info("Payload == > " + message.getPayload());
try {
stateMachine.getExtendedState().getVariables().put("imei", user);
stateMachine.getExtendedState().getVariables().put("fromState", stateMachine.getState().getId());
stateMachine.getExtendedState().getVariables().put("eventName", message.getPayload());
if(null!= message.getHeaders().get("templates"))
stateMachine.getExtendedState().getVariables().put("templates", message.getHeaders().get("templates"));
if(null!= message.getHeaders().get("ttl"))
stateMachine.getExtendedState().getVariables().put("ttl", message.getHeaders().get("ttl"));
} catch (Exception e) {
log.error(e.getMessage(), e);
}
// check if state is properly restored...
log.info("Current State " + stateMachine.getState().toString());
feedMachine(stateMachine, user, message);
log.info("handler exited");
} catch (Exception e) {
log.error(e.getMessage(), e);
}
// TODO: save persistant state..
}
private void feedMachine(StateMachine<String, String> stateMachine, String user, GenericMessage<String> event)
throws Exception {
stateMachine.sendEvent(event);
System.out.println("persist machine --- > state :" + stateMachine.getState().toString());
redisStateMachinePersister.persist(stateMachine, "testprefixSw:" + user);
}
private StateMachine<String, String> resetStateMachineFromStore(StateMachine<String, String> stateMachine,
String user) throws Exception {
StateMachine<String, String> machine = redisStateMachinePersister.restore(stateMachine, "testprefixSw:" + user);
System.out.println("restore machine --- > state :" + machine.getState().toString());
return machine;
}
Actions
#Bean
public Action<String, String> sendBasicTemplate() {
// Action handler...
return new Action<String, String>() {
#Override
public void execute(StateContext<String, String> context) {
// MP: variables are the right way to do
String imeiNo = (String) context.getExtendedState().getVariables().get("imei");
String template = (String) context.getMessageHeader("template");
log.info("sending basic template " + template + " to " + imeiNo);
findTemplateNSend(context, template, imeiNo);
xbossBalanceCheck(context, imeiNo, "Direct Query");
setRiskyState(context, "testprefixSw:RISKY_StateBasic_WFT_Timeout" + imeiNo, 0);
}
};
}
#Bean
public Action<String, String> processBasicTemplateReply() {
// Action handler...
return new Action<String, String>() {
#Override
public void execute(StateContext<String, String> context) {
log.info("Result for basic template processing started");
log.info(context.getStateMachine().getState().getIds().toString());
String imeiNo = (String) context.getExtendedState().getVariables().get("imei");
saveDirectValues(context, imeiNo);
String fromState = (String) context.getExtendedState().getVariables().get("fromState");
String eventName = (String) context.getExtendedState().getVariables().get("eventName");
long trId = (Long) context.getMessageHeader("processId") != null? (Long) context.getMessageHeader("processId") : 0;
String key = "testprefixSw:RISKY_StateBasic_WFT_Timeout" + imeiNo;
log.info("*Going to delete if exists key ==>" + key);
if (clearRiskyStateIfSet(context, key)) {
log.info("------------------------------Jedis Exists");
sendSubscriberEventLog(imeiNo, fromState, context.getStateMachine().getState().getId(), trId, eventName, false, "Query Event Success");
}
// mark as success sent
context.getStateMachine().sendEvent("SEQUENCE_COMPLETE");
}
};
}
#Bean
public Action<String, String> timeoutLogAction() {
// Action handler...
return new Action<String, String>() {
#Override
public void execute(StateContext<String, String> context) {
// log.info("timeout log Action");
String imeiNo = (String) context.getStateMachine().getExtendedState().getVariables().get("imei");
// String imeiNo = (String)
// context.getExtendedState().getVariables().get("imei");
String fromState = (String) context.getExtendedState().getVariables().get("fromState");
String eventName = (String) context.getExtendedState().getVariables().get("eventName");
long trId = (Long) context.getMessageHeader("processId") != null ? (Long) context.getMessageHeader("processId") : 0;
String key = "testprefixSw:RISKY_StateBasic_WFT_Timeout" + imeiNo;
log.info("*Going to delete if exists key ==>" + key);
if (clearRiskyStateIfSet(context, key)) {
log.info("------------------------------Jedis Exists at timeout. Event Failed");
sendSubscriberEventLog(imeiNo, fromState, context.getStateMachine().getId(), trId, eventName, true, "Direct Query Failed due to Timeout");
sendAlert(imeiNo, EventPriority.NORMAL, "Direct Query Failed due to Timeout");
}
}
};
}
So based on the above, Is there anything I'm missing so that the created state machines are not collected by garbage? or any other explanation as to why memory is being consumed with each request and it never gets released?

RequestParams params.put method doesn't receive integer

I'm using Rest service to get a list of States of a Country (countryid) from database. The service runs well in Tomcat but it doesn't run in Android. Below is the code to invoke the Rest service. The params.put("countryid",countryID) method doesn't receive integer. Could you please help me over come this issue?
public void getDataForStateSpinner(){
//new StateSpinnerAsync().execute();
System.out.println("getDataForStateSpinner(), spnCountry adapter: " + spnCountry);
System.out.println("spnCountry.getSelectedItem(): " + spnCountry.getSelectedItem());
if (spnCountry.getSelectedItem()!=null){
System.out.println("getDataForStateSpinner(), spnCountry adapter isn't empty");
final Country country = (Country) spnCountry.getSelectedItem();
int countryID = country.getCountryID();
RequestParams params = new RequestParams();
if (countryID>=0){
params.put("countryid", countryID);
invokeWS_State(params);
} else{
Toast.makeText(mParent.get(), "Can not get CountryID", Toast.LENGTH_LONG).show();
}
}
}
public void invokeWS_State(RequestParams params){
System.out.println("Inside invokeWS_State");
AsyncHttpClient client = new AsyncHttpClient();
System.out.println("Inside invokeWS_State, client: " + client);
System.out.println("Inside invokeWS_State onSuccess, params: " + params);
client.get(stateURL, params, new AsyncHttpResponseHandler(){
#Override
public void onSuccess(String respond){
try{
System.out.println("Inside invokeWS_State onSuccess, stateURL: " + stateURL);
System.out.println("Inside invokeWS_State onSuccess, respond:" + respond);
JSONArray jsonArr = new JSONArray(respond);
//JSONObject jsonObject = jsonArr.getJSONObject(0);
System.out.println("Inside invokeWS_State onSuccess jsonArr:" + jsonArr);
String stateList = jsonArr.toString();
System.out.println("Inside invokeWS_State onSuccess stateList:" + stateList);
states = (ArrayList<State>) fromJasonToJava_State(stateList);
for (State state : states) {
System.out.println("State id: " + state.getStateID() + " name: " + state.getStateName());
}
spnStateAdapter = new ArrayAdapter<State>(mParent.get(), android.R.layout.simple_spinner_dropdown_item, states);
spnStateAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnState.setAdapter(spnStateAdapter);
} catch (JSONException e){
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public void onFailure(int statusCode, Throwable error, String content){
String resultString = null;
if (statusCode == 404){
resultString = "Requested resource not found";
Toast.makeText(mParent.get(), resultString, Toast.LENGTH_LONG).show();
} else if (statusCode == 500) {
resultString = "Something went wrong at server end!";
Toast.makeText(mParent.get(), resultString, Toast.LENGTH_LONG).show();
} else {
resultString = "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]";
Toast.makeText(mParent.get(), resultString, Toast.LENGTH_LONG).show();
}
}
});
}
And below is the Rest service which runs well in Tomcat:
#Path("/State")
public class StatesResource {
#GET
#Path("/GetStates")
#Produces(MediaType.APPLICATION_JSON)
//http://localhost:8080/com.fms.FMSRestfulWS/State/GetStates?countryid=1
public String getStates(#DefaultValue("1") #QueryParam("countryid") int countryID){
String states = null;
try{
ArrayList<State> feedData = null;
StateLoading stateLoading = new StateLoading();
feedData = stateLoading.getAllStatesForCountry(countryID);
Gson gson = new Gson();
states = gson.toJson(feedData);
}catch (Exception e){
System.out.println("System Error " + e);
}
return states;
}
}
Do only one change, it will work perfect.
params.put("countryid", ""+countryID);

Categories

Resources