这是我从互联网上获得的代码,我需要它用于我自己的应用程序。我正在尝试使用该示例制作应用程序。不同之处在于我为此命名JSONTask
创建了一个单独的类。我的getApplicationContex()
函数出错了。请帮帮我。
import android.os.AsyncTask;
import android.widget.ListView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import navdrawerexample1.models.MovieModel;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class JSONTask extends AsyncTask<String,String,List<MovieModel>>
{
private ListView lvMovies;
@Override
protected List<MovieModel> doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url= new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line ="";
while ((line = reader.readLine())!=null){
buffer.append(line);
}
String finalJason = buffer.toString();
JSONObject parentObject = new JSONObject(finalJason);
JSONArray parentArray = parentObject.getJSONArray("movies");
List<MovieModel> movieModelList = new ArrayList<>();
//StringBuffer finalBufferData = new StringBuffer();
for (int i=0; i<parentArray.length();i++) {
JSONObject finalObject = parentArray.getJSONObject(i);
MovieModel movieModel = new MovieModel();
movieModel.setMovie(finalObject.getString("movie"));
movieModel.setYear(finalObject.getInt("year"));
movieModel.setRating((float) finalObject.getDouble("rating"));
movieModel.setDuration(finalObject.getString("duration"));
movieModel.setDirector(finalObject.getString("director"));
movieModel.setTagline(finalObject.getString("tagline"));
movieModel.setImage(finalObject.getString("image"));
movieModel.setStory(finalObject.getString("story"));
List<MovieModel.cast> castList = new ArrayList<>();
for (int j=0; j<finalObject.getJSONArray("cast").length();j++){
MovieModel.cast cast = new MovieModel.cast();
cast.setName(finalObject.getJSONArray("cast").getJSONObject(j).getString("name"));
castList.add(cast);
}
movieModel.setCastList(castList);
//adding the final object in the list
movieModelList.add(movieModel);
}
return movieModelList;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(List<MovieModel> result) {
super.onPostExecute(result);
//This is where I get the error
MovieAdapter adapter = new MovieAdapter(getApplicationContext(),R.layout.row,result);
lvMovies.setAdapter(adapter);
//TODO need to set data to the list
}
}
答案 0 :(得分:10)
问题是您在不扩展getApplicationContext()
或其子类(Context
等)的类中调用Activity
。您应该将Context
或其子类传递给JSONTask
构造函数。此外,我不知道您在初始化lvMovies
的位置 - 并且您可能会NullPointerException
lvMovies.setAdapter(adapter);
- 我的建议是您应该将此传递给您JSONTask
{1}}构造函数:
private Context mContext;
private ListView lvMovies;
public JSONTask (Context context, ListView lstView){
this.lvMovies = lstView;
this.mContext = context;
}
这样在onPostExecute
你可以做类似的事情:
MovieAdapter adapter = new MovieAdapter(mContext,R.layout.row,result);
lvMovies.setAdapter(adapter);
我希望这会有所帮助。
答案 1 :(得分:1)
您无法从asynTask访问getApplicationContext()
因为getApplicationContext()
在AsyncTask中不存在。
创建一个构造函数并将Context传递给此类。在JSONTask类中使用它。
我建议您使用Volley库而不是AsyncTask。
答案 2 :(得分:0)
创建一个构造函数并传递一个像
这样的Context变量public class JSONTask extends AsyncTask<String,String,List<MovieModel>>{
public Context mContext;
public JSONTask(Context context){
this.mContext = context;
}
.
.
.
现在从您的活动/服务创建JsonTask对象
JSONTask task = new JSONTask(getApplicationContext());
现在您可以在需要传递上下文的地方使用mContext。
答案 3 :(得分:0)
首先声明上下文,
Activity context;
然后
MovieAdapter adapter = new MovieAdapter(context.getApplicationContext(),R.layout.row,result);
lvMovies.setAdapter(adapter);
我希望这对你有帮助......