我在Android Fragment类中执行AsyncTask
时遇到了一些问题。
在onClick
方法中,我使用以下代码new BackgroundTaskPost().execute();
我试图以不同的方式形成我的AsyncTask
,但它们都不起作用。
我最近的尝试如下:
public class BackgroundTaskPost extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute(){
SharedPreferences preferenceLogin = getActivity().getSharedPreferences(loginPreference, Context.MODE_PRIVATE);
userName = preferenceLogin.getString("UserName", null);
password = preferenceLogin.getString("Password", null);
SharedPreferences sharedPreferences = getActivity().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
radioId1 = sharedPreferences.getString("check1", " ");
radioId2 = sharedPreferences.getString("check2", " ");
radioId3 = sharedPreferences.getString("check3", " ");
radioId4 = sharedPreferences.getString("check4", " ");
radioId5 = sharedPreferences.getString("check5", " ");
radioId6 = sharedPreferences.getString("check6", " ");
radioId7 = sharedPreferences.getString("check7", " ");
radioId8 = sharedPreferences.getString("check8", " ");
radioId9 = sharedPreferences.getString("check9", " ");
radioId10 = sharedPreferences.getString("check10", " ");
radioId11 = sharedPreferences.getString("check11", " ");
//fev1 = pref.getFloat("Result", 0);
outputs = new Outputs[13];
outputs[0] = new Outputs("387.WEIGHT", "Float", "25.3");
outputs[1] = new Outputs("383.FIELD", "String", radioId1);
outputs[2] = new Outputs("384.FIELD", "String", radioId2);
outputs[3] = new Outputs("381.FIELD", "String", radioId3);
outputs[4] = new Outputs("389.FIELD", "String", radioId4);
outputs[5] = new Outputs("388.LF#FEV1", "Float", "0.99");
outputs[6] = new Outputs("390.FIELD", "String", radioId5);
outputs[7] = new Outputs("378.FIELD", "String", radioId6);
outputs[8] = new Outputs("379.FIELD", "String", radioId7);
outputs[9] = new Outputs("380.FIELD", "String", radioId8);
outputs[10] = new Outputs("382.FIELD", "String", radioId9);
outputs[11] = new Outputs("385.FIELD", "String", radioId10);
outputs[12] = new Outputs("386.FIELD", "String", radioId11);
}
@Override
protected String doInBackground(String... params){
postRequest = new HttpPostRequest(userName, password);
String content = postRequest.postQuestionnaire(59, "Månedkontrol", "1.0", outputs);
return content;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(getActivity(), result, Toast.LENGTH_LONG).show();
}
}
我的问题是,我从未来过onPostExecute
,因为应用程序崩溃,导致logcat出现错误。
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.RuntimeException: Failed : HTTP error code : 500
at com.example.OpenTeleStandaloneApplication.HttpIntegration.HttpPostRequest.postRequest(HttpPostRequest.java:55)
at com.example.OpenTeleStandaloneApplication.HttpIntegration.HttpPostRequest.postQuestionnaire(HttpPostRequest.java:104)
at com.example.OpenTeleStandaloneApplication.ApplicationInterface.cfqrlunge7$BackgroundTaskPost.doInBackground(cfqrlunge7.java:169)
at com.example.OpenTeleStandaloneApplication.ApplicationInterface.cfqrlunge7$BackgroundTaskPost.doInBackground(cfqrlunge7.java:128)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
我可以告诉我有几次尝试在Eclipse的控制台应用程序中使用相同的代码,它在没有问题的情况下工作得很好。所以我可以得出结论,问题出现在AsyncTask
。
HttpRequest:
public String getInput(int id, String name, String version, Outputs[] outputs){
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
String retVal = "{\"QuestionnaireId\":" + id + "," +
"\"date\":\"" + dateFormat.format(new Date()) + "\"," +
"\"name\":\"" + name + "\"," +
"\"version\":\"" + version + "\"," +
"\"output\":[";
for (int i = 0; i < outputs.length; i++) {
retVal += "{\"name\":\""+ outputs[i].getName() +
"\",\"type\":\"" + outputs[i].getType() +
"\",\"value\":" + outputs[i].getValue() +
"}";
if(i < outputs.length - 1)
retVal += ",";
}
retVal += "]}";
return retVal;
}
public String postQuestionnaire(int id, String name, String version, Outputs[] outputs){
return postRequest("rest/questionnaire/listing", getInput(id, name, version, outputs));
}
希望任何人都可以帮助我,过去两天我在这个问题上花了很多时间。 非常感谢。
答案 0 :(得分:0)
观察 - 有几个非常好的网络/休息库,所以没有理由用低级别的HttpRequest和朋友推出自己的库。处理简单HTTP 500错误的问题强调了这一点。
此外,尽管获得了很多&#34;积极的新闻&#34;,AsyncTask是一个尝试进行网络访问的糟糕地方。我认为它是一个根本不应该被使用的结构,因为它不是活动生命周期意识,但我离题了。
我建议您查看上述库中的一个,例如Retrofit或Volley。他们将为您提供一个处理许多问题的框架,而不是重新发明轮子。