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;
    }
}