share

Thursday, 28 January 2016

Failed to load resource: the server responded with a status of 406 (Not Acceptable) in spring mvc ajax call

This error can be solve by these tricks .

1. use @EnableWebMvc with  spring 4.0 or above.

 @Controller
@EnableWebMvc
@RequestMapping(value = "/articles/action",  produces="application/json")
public class UserController {

}


2. use  produces="application/json" in @RequestMapping


3. add these dependency or jar if missing

<dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.2.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.2.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.2.3</version>
    </dependency>

Tuesday, 19 January 2016

Rest Template: Could Not Extract Response – No Suitable HttpMessageConverter Found for Response Type [X] and Content Type

When invoking a web service using RestTemplate:-

restTemplate.getForObject("http://www.server.com/api", Bean[].class));
… the following exception occurs:-

Exception in thread "main" org.springframework.web.client.RestClientException:
Could not extract response: no suitable HttpMessageConverter found for response
type [class [LMyBean;] and content type [application/json;charset=UTF-8]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:1110)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:1572)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:1530)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:1237)



SOLUTION

If the content type is JSON, add the following dependency:-

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.6.0</version>
</dependency>


Saturday, 16 January 2016

how to use onclick and ontouch listener both for diffrent functionality


There is code for different functionality on onclick and also different functionality for start and stop event of ontouch .

public class MyFragment extends BaseFragment implements View.OnTouchListener {

    @Override
    public boolean onTouch(final View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                prev_timestamp = System.currentTimeMillis();

            case MotionEvent.ACTION_UP:
                current_timestamp = System.currentTimeMillis();
                upcount++;
                if (current_timestamp - prev_timestamp < 250) {

                    if (upcount == 2) {
                        upcount = 0;
                        Log.d(TAG, "click event");
//here code for onClick event
                    } else {

                        new AsyncTask<Void, Void, Void>() {

                            @Override
                            protected Void doInBackground(Void... params) {
                                try {
                                    Thread.sleep(500);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                return null;
                            }

                            @Override
                            protected void onPostExecute(Void aVoid) {
                                if (upcount != 0) {
                                    TOUCH_END=false;
                                    Log.d(TAG, "touch start");
   //here code for onTouchStart event
                                }
                                super.onPostExecute(aVoid);
                            }
                        }.execute();

                    }
                } else {
                    upcount = 0;
                    Log.d(TAG, "touch end");
                    //here code for onTouchEnd event
                }

            case MotionEvent.ACTION_CANCEL:
                //here code for cancel event if you want

        }
        return true;
    }
}

Monday, 14 December 2015

How to Convert Enumeration to Arraylist - Java Example

We can convert enumeration to arraylist in java with  the help list() method of Collections.

package javaproficeincy;

import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;

public class EnumerationToList {
public static void main(String[] args) {
Vector<String> vt = new Vector<String>();
        vt.add("java");
        vt.add("php");
        vt.add("array");
        vt.add("string");
        vt.add("c");
        Enumeration<String> enm = vt.elements();
        List<String> ll = Collections.list(enm);
        System.out.println("List elements: "+ll);
}

}


Output:

List elements: [java, php, array, string, c]

Related Posts :


Monday, 7 December 2015

How to Send message from one user to other ( chat example) In xmpp

We have knew how to add roster in xmpp server . Now we will send message from one user to other.
There is simple example for creating chat. There we will send a message using smack api and xmpp server. You can can visit how to install xmpp server.

package com.javaproficiency.demo;

import java.util.Collection;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;



public class SendMessageInChat {

 public static void main(String[] args) {

   AddRoster addRoster = new AddRoster();
 
XMPPConnection connection = addRoster.Connect();
try {
connection.login("yogesh@localhost","123");
System.out.println("Login");

Chat chat = connection.getChatManager().createChat("shivam@localhost", new MessageListener() {

@Override
            public void processMessage(Chat chat, Message message) {
                // Print out any messages we get back to standard out.
                System.out.println("Received message: " + message);
            }

        });
        try {
chat.sendMessage("How are you dear !!");
System.out.println(" Send Message succesfully");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

           connection.disconnect();

} catch (XMPPException e) {

e.printStackTrace();
}
}


private void loginUser(XMPPConnection connection) {
// TODO Auto-generated method stub

}


public XMPPConnection Connect() {
 
  ConnectionConfiguration config = new ConnectionConfiguration(
  "localhost", 5222);

  /*
  * ConnectionConfiguration config = new ConnectionConfiguration(
  * "192.163.2.200", 5222);
  */
  XMPPConnection connection = new XMPPConnection(config);
  try {
  connection.connect();
  } catch (XMPPException e) {
 
  e.printStackTrace();
  }
  return connection;
  }



}



After execution of this you can see message in xmpp client spark as

How to chat in xmpp server




How to remove Roster or Friend in xmpp server

In last post we have learn how to add roster in xmpp.  Now we will remove roster from roster list of any user. For  first you should  add roster in roster list of user. Now We get roster list of user and compare jabber id of roster and remove it using  removeEntry( RosterEntry ) method  . After remove roster you check roster using this article How to get roster of jabber id in xmpp server .

package com.javaproficiency.demo;

import java.util.Collection;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;



public class RemoveRoster {

  public static void main(String[] args) {

    AddRoster addRoster = new AddRoster();
 
XMPPConnection connection = addRoster.Connect();
try {
connection.login("yogesh@localhost","123");
System.out.println("Login");
Roster roster = connection.getRoster();
String removeJID = "shivam@localhost";
Collection<RosterEntry> entries = roster.getEntries();
for (RosterEntry entry : entries) {
          // remove roster whith removeJID
if (removeJID.equals(entry.getUser())) {
roster.removeEntry(entry);
}
}

           connection.disconnect();

} catch (XMPPException e) {

e.printStackTrace();
}
}


public XMPPConnection Connect() {
  
   ConnectionConfiguration config = new ConnectionConfiguration(
   "localhost", 5222);

   /*
    * ConnectionConfiguration config = new ConnectionConfiguration(
    * "192.163.2.200", 5222);
    */
   XMPPConnection connection = new XMPPConnection(config);
   try {
   connection.connect();
   } catch (XMPPException e) {
  
   e.printStackTrace();
   }
   return connection;
   }



}



how to add roster in xmpp using smack api

In last article we have learn how to login on xmpp server using smack api . Now In this article we will learn how to add friends( roster ) in buddy using smack api.For add roster in xmpp first you should make two account. See It how to resister on xmpp server if you do not know how to register user on xmpp server. after that you can send roster request using below code . 


package com.javaproficiency.demo;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.Roster.SubscriptionMode;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;

public class AddRoster {

 public static void main(String[] args) {

   AddRoster addRoster = new AddRoster();
 
XMPPConnection connection = addRoster.Connect();
try {

                     // user yogesh login on xmpp server
connection.login("yogesh@localhost","123");
System.out.println("Login");

           Roster roster = connection.getRoster();
           // yogesh send roster request to shivam
           roster.createEntry("shivam@localhost","myking",null);

           System.out.println(" send roster request");
           connection.disconnect();

} catch (XMPPException e) {

e.printStackTrace();
}
}


public XMPPConnection Connect() {
 
  ConnectionConfiguration config = new ConnectionConfiguration(
  "localhost", 5222);

  /*
  * ConnectionConfiguration config = new ConnectionConfiguration(
  * "192.163.2.200", 5222);
  */
  XMPPConnection connection = new XMPPConnection(config);
  try {
  connection.connect();
  } catch (XMPPException e) {
 
  e.printStackTrace();
  }
  return connection;
  }


}



After execution  of this code you can see this output :

 Login
 
send roster request


And  


how to add roster in xmpp server