在下面的类中,使用异步内部类函数 doInBackground 没有被命中。在调用 rpi.excute 然后尝试通过 HTTP 下载的 getQuestionnaireBO 获取数据时,我在 AndroidManifest.xml 和其他配置中拥有 Internet 权限
public class RestApiClient {
private final static String SERVICE_URI = "http://182.50.154.23/Dattab.Device.Rest/ServiceClass/RestDeviceService.svc";
private QuestionnaireBO resultQuestionnaireBO ;
public void Questionnairedownload(int QuestionnaireId ) {
String url=SERVICE_URI+"/DownloadQuestionnaireData/"+QuestionnaireId;
// InputStream source = retrieveStream(url);
// Gson gson = new Gson();
// Reader reader = new InputStreamReader(source);
// QuestionnaireBO response = gson.fromJson(reader, QuestionnaireBO.class);
ChildThreadQuestionnairedownload ctqd = new ChildThreadQuestionnairedownload();
ctqd.execute(url);
// return null ;
}
public QuestionnaireBO getQuestionnaireBO() {
return resultQuestionnaireBO;
}
private class ChildThreadQuestionnairedownload extends AsyncTask<String,Integer,QuestionnaireBO> {
@Override
protected QuestionnaireBO doInBackground(String... params) {
String url=params[0];
Gson gson = new Gson();
DefaultHttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(url);
try {
HttpResponse getResponse = client.execute(getRequest);
final int statusCode = getResponse.getStatusLine().getStatusCode();
if(statusCode != HttpStatus.SC_OK) {
Log.w(getClass().getSimpleName(),
"Error " + statusCode + " for URL " + url);
return null;
}
HttpEntity getResponseEntity = getResponse.getEntity();
Reader reader = new InputStreamReader(getResponseEntity.getContent());
QuestionnaireBO response = gson.fromJson(reader, QuestionnaireBO.class);
return response ;
}
catch(IOException e){
e.printStackTrace();
return null;
}
}
protected void onPostExecute(QuestionnaireBO response) {
resultQuestionnaireBO=response;
}
}
}