1

I have set an OnclickListener to an ImageView and when clicks performs an asyncTask, it works well on devices running below Api 5.0, but do not work at all in Android Api 5.0 devices.

Async Task:

public class SendName extends AsyncTask<String, String, String> {

    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... strings) {

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://example.com/web.php");

            String user = "John";

            try {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);


                   nameValuePairs.add(new BasicNameValuePair("user", user));

                   httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                   httpclient.execute(httppost);


        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String lengthOfFile) {

    }

} 

ImageView XML:

<ImageView
android:id="@+id/send"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/send" />

OnClickListener:

ImageView send_img = (ImageView) findViewById(R.id.send);
send_img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new sendName().execute()

        }
    });
Floern
  • 33,559
  • 24
  • 104
  • 119
Pdee As-Diddy
  • 147
  • 2
  • 14

2 Answers2

0

Did you try to debug the code if it goes inside the doInBackground or not. If it runs then their might be some problem with HttpClient which is deprecated in Android 6. You can first check and confirm if this is HttpClient problem, if it is then add following line in your app gradle

    android {
        useLibrary 'org.apache.http.legacy'
    }

or you can also change and implement HttpURLConnection

Ani
  • 1,041
  • 6
  • 15
0

Put the following attribute in your XML inside image view

android:clickable="true"

Snake
  • 14,228
  • 27
  • 117
  • 250