我有这个AsyncTask
public class clientesAsyncTask extends AsyncTask<Void, Void,String> {
private String user,password,response;
private String clientesUrl="example.org/services/data.php";
private int distance;
ArrayList<Cliente> listaClientes;
public clientesAsyncTask(String user,String password) {
this.user=user;
this.password=password;
response="";
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected String doInBackground(Void... params) {
try {
URL url = new URL(clientesUrl);
HttpURLConnection hConnection = (HttpURLConnection)url.openConnection();
HttpURLConnection.setFollowRedirects(true);
hConnection.setReadTimeout(10000);
hConnection.setConnectTimeout(15000);
hConnection.setDoOutput(true);
hConnection.setDoInput(true);
hConnection.setRequestMethod("POST");
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("usuario", user)
.appendQueryParameter("password", password);
String query = builder.build().getEncodedQuery();
OutputStream os = hConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
hConnection.connect();
int responseCode=hConnection.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br=new BufferedReader(new InputStreamReader(hConnection.getInputStream()));
while ((line = br.readLine()) != null) {
response+=line;
}
hConnection.disconnect();
}
else {
response="";
throw new Exception(responseCode+"");
}
} catch (Exception e) {
e.printStackTrace();
}
Log.d("Response", response);
return response;
}
@Override
protected void onPostExecute(String response) {
super.onPostExecute(response);
}
}
我在另一个AsyncTask之后调用它
loginTask=new loginAsyncTask(user,password);
String autenticacion=loginTask.execute((Void)null).get();
clientesAsyncTask clientesTask= new clientesAsyncTask(user,pass);
String data =clientesTask.execute((Void)null).get();
但是“resposne”正在从adb记录器返回消息,而不是从请求返回响应。 我使用完全相同的asynctask whith一个不同的名称来执行AsyncTask但第二个只返回adb日志消息,为什么? 我在浏览器上测试了请求并且没有错误。 造成这种情况的原因是什么?
编辑1 我的AsyncTask与其他无关。 我为每个任务制作了测试文件,问题仍在继续。