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