I have a list of buttons and i want that each fire a loading posting the unique preload of the elements.The problem is that the first complete the running in 200ms, the second in 3sec, and the 3rd never stop loading,... the async don't end completely. I don't know if is a problem of parallelism of the treads. Anyway using rest console everything works perfectly.
public class URLDataReader extends AsyncTask<URL, String, String> {
Context mContext;
OnAsyncTaskComplete listener;
public URLDataReader(Context context, OnAsyncTaskComplete lisener) {
mContext = context;
listener = lisener;
}
@Override
protected String doInBackground(URL... data) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(data[0].toString());
HttpResponse response = null;
try {
List<NameValuePair> nameValuePairs = Globals.postUrl();
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//SETTA I POST IN GLOBALS
//nameValuePairs.add(new BasicNameValuePair("username", "s.alberti"));
//nameValuePairs.add(new BasicNameValuePair("password", "65.VALE.61"));
//execute http post
response = httpclient.execute(httppost);
} catch (Exception e) {
Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_LONG).show();
}
try {
return convertHttpResponseToString(response);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
listener.OnAsyncTaskComplete(s);
}
private String convertHttpResponseToString(HttpResponse response) throws IOException {
InputStream responseStream = response.getEntity().getContent();
Scanner scanner = new Scanner(responseStream, "UTF-8");
String responseString = scanner.useDelimiter("\\Z").next();
scanner.close();
return responseString;
}
}
enter code here
GLOBAL
public static List<NameValuePair> postUrl(){
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("username", "s.alberti"));
nameValuePairs.add(new BasicNameValuePair("password", "Vale6165"));
return nameValuePairs;
}
FRAGMENT CALLING THE ASYNCTASK
try {
String ex = extractUni(getContext());
URL address = new URL("https://api.myuni.it/"+ex+"/login/");
Toast.makeText(getContext(), address+"",+Toast.LENGTH_SHORT).show();
URLDataReader pp = new URLDataReader(getContext(), this);
pp.execute(address);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
HTTPClient
withDefaultHttpClient
which has been depreciated for quite a while! I recommend eitherHttpUrlConnection
orOkHttp
. Secondly, you might be having an issue server side. I strongly suggest that you set a time-out in the connect!con.setConnectTimeout(5000)