我有一个按钮列表,我希望每个发布一个加载发布元素的唯一预加载。问题是第一个完成200ms的运行,第二个在3秒,第三个永远不会停止加载。 ..异步不完全结束。我不知道是否存在胎面平行度问题。无论如何使用休息控制台一切都很完美。
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;
}
片段呼吁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();
}
}