我正在尝试编写一个应用程序,该应用程序可以访问我的朋友作为笑话创建的网页,用户可以在其中为愚蠢的事情互相给予“奖牌”。一个例子是该网站的所有者给了另一个朋友,我称他为鲍勃一枚名为“密码天才”的奖章,上面写着“忘记了他的麻省理工学院招生密码,一般来说密码很差。”。奖牌以 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, "this will print to console"); 实际上打印,而不是Log.v(TAG, json)
;。我查看了其他教程,它们看起来几乎相同。我无法弄清楚我的生活。任何帮助将不胜感激。