Reputation: 3
I'm trying to write an app that accesses a webpage my friend created as a joke where users can give "medals" to one another for stupid things. an example is the owner of the site gave another friend whom i shall call bob a medal called "password genius" with the description "Forgot his MIT admissions password and is just bad at passwords in general.". the medals are retrieved in JSON format, and the above example would be
[...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....]
. I want to retrieve these using an AsyncTask in my app, but i get the weirdest compiler errors. my code is, in a nutshell,
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
}
}
}
if I try to do that, I get
[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
however, if I replace
private class GetMedalsTask extends AsyncTask<String, void, String>
with
private class GetMedalsTask extends AsyncTask//<String, void, String>
it will compile, but only the Log.v(TAG, "this will print to console"); actually prints, not the Log.v(TAG, json)
;. I have looked up other tutorials, and they look pretty much identical. i cant figure out for the life of me. any help would be greatly appreciated.
Upvotes: 0
Views: 1474
Reputation: 133560
This
AsyncTask<String, void, String>
should be
AsyncTask<String, Void, String> // V is capital
Also
@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
}
The result of doInBackground
computation is a paramter to onPostExecute
.
http://developer.android.com/reference/android/os/AsyncTask.html.
Check the topic under AsyncTask generic types.
Also check the topic under the section The 4 steps.
Upvotes: 1