我正在尝试编写一个应用程序来访问我的朋友创建的网页作为一个笑话,用户可以为愚蠢的事情互相赠送“奖牌”。一个例子是该网站的所有者给了另一个朋友,我将其称为“密码天才”,其描述为“忘记了他的麻省理工学院招生密码,而且一般情况下密码不好”。奖牌以JSON格式检索,上面的例子是
[...other medals....,{"medalid":"21","uid":"bob","title":"Password Genius","description":"Forgot his MIT admissions password and is just bad at passwords in general.","givenby":"fred"}, ...other medals....]
。我想在我的应用程序中使用AsyncTask检索这些,但我得到了最奇怪的编译器错误。简而言之,我的代码是
public class MedalMania extends activity
{
public static final String TAG = "[MedalMania]";
public void lookupButtonPressed(View view)
{
(new GetMedalsTask()).execute(((EditText)R.findViewById(R.id.name_box))getText().toString());
}
private class GetMedalsTask extends AsyncTask<String, void, String>
{
@Override
protected String doInBackground(String... params)
{
Log.v(TAG, "This will print to console");
return "i dont need to type my real code, even this simplified form wont work";
}
@Override
protected void onPostExecute(String json)
{
Log.v(TAG, json);//This wont print to console
}
}
}
如果我尝试这样做,我会
[javac] Compiling 4 source files to /home/alex/droid_apps/MedalMania/bin/classes
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:59: illegal start of type
[javac] private class GetMedalsTask extends AsyncTask<String, void, String>
[javac] ^
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:59: '{' expected
[javac] private class GetMedalsTask extends AsyncTask<String, void, String>
[javac] ^
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:59: <identifier> expected
[javac] private class GetMedalsTask extends AsyncTask<String, void, String>
[javac] ^
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:62: ';' expected
[javac] protected String doInBackground(String... params)
[javac] ^
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:62: not a statement
[javac] protected String doInBackground(String... params)
[javac] ^
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:62: ';' expected
[javac] protected String doInBackground(String... params)
[javac] ^
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:62: not a statement
[javac] protected String doInBackground(String... params)
[javac] ^
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:62: ';' expected
[javac] protected String doInBackground(String... params)
[javac] ^
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:76: illegal start of type
[javac] protected void onPostExecute(String json)
[javac] ^
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:76: ';' expected
[javac] protected void onPostExecute(String json)
[javac] ^
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:76: ')' expected
[javac] protected void onPostExecute(String json)
[javac] ^
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:76: not a statement
[javac] protected void onPostExecute(String json)
[javac] ^
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:76: ';' expected
[javac] protected void onPostExecute(String json)
[javac] ^
[javac] /home/alex/droid_apps/MedalMania/src/com/alex/medalmania/MedalMania.java:81: reached end of file while parsing
[javac] }
[javac] ^
[javac] 14 errors
然而,如果我更换
private class GetMedalsTask extends AsyncTask<String, void, String>
与
private class GetMedalsTask extends AsyncTask//<String, void, String>
它将编译,但只有Log.v(TAG,“这将打印到控制台”);实际打印,而不是Log.v(TAG, json)
;。我已经查找了其他教程,它们看起来非常相似。我无法弄清楚我的生活。任何帮助将不胜感激。
答案 0 :(得分:1)
void
不是参考类型..
您需要使用Void作为参考类型
答案 1 :(得分:1)
此
AsyncTask<String, void, String>
应该是
AsyncTask<String, Void, String> // V is capital
另外
@Override
protected void onPostExecute(String json)
{
Log.v(TAG, json);//This wont print to console
}
shoudl be
@Override
protected void onPostExecute(String json)
{
super.onPostExecute(json);
Log.v(TAG, json);//This wont print to console
}
doInBackground
计算的结果是onPostExecute
的参数。
http://developer.android.com/reference/android/os/AsyncTask.html。
检查AsyncTask泛型类型下的主题。
另请查看“4个步骤”部分下的主题。