如何在片段中使用 GetJsonFromUrlTask.java
Posted
技术标签:
【中文标题】如何在片段中使用 GetJsonFromUrlTask.java【英文标题】:How to use GetJsonFromUrlTask.java in a fragment 【发布时间】:2018-02-21 16:15:05 【问题描述】:我从一个网站上找到了下面的代码,它似乎对我的 android 应用程序有帮助。但问题是,下面的代码仅用于活动,我需要在片段中运行代码。顺便说一句,我的应用程序是关于从在线数据库中下载图像并将其存储到列表视图中。
我该怎么办?
这是我要使用的代码:
public class GetJsonFromUrlTask extends AsyncTask<Void, Void, String>
private Activity activity;
private String url;
private ProgressDialog dialog;
private final static String TAG = GetJsonFromUrlTask.class.getSimpleName();
public GetJsonFromUrlTask(Activity activity, String url)
super();
this.activity = activity;
this.url = url;
@Override
protected void onPreExecute()
super.onPreExecute();
// Create a progress dialog
dialog = new ProgressDialog(activity);
// Set progress dialog title
dialog.setTitle("Getting JSON DATA");
// Set progress dialog message
dialog.setMessage("Loading...");
dialog.setIndeterminate(false);
// Show progress dialog
dialog.show();
@Override
protected String doInBackground(Void... params)
// call load JSON from url method
return loadJSON(this.url).toString();
@Override
protected void onPostExecute(String result)
((CategoryListViewActivity) activity).parseJsonResponse(result);
dialog.dismiss();
Log.i(TAG, result);
public JSONArray loadJSON(String url)
// Creating JSON Parser instance
JSONGetter jParser = new JSONGetter();
// getting JSON string from URL
JSONArray json = jParser.getJSONFromUrl(url);
return json;
private class JSONGetter
private InputStream is = null;
private JSONArray jObj = null;
private String json = "";
// constructor
public JSONGetter()
public JSONArray getJSONFromUrl(String url)
// Making HTTP request
try
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
catch (UnsupportedEncodingException e)
e.printStackTrace();
catch (ClientProtocolException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
try
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"),
8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
sb.append(line + "\n");
is.close();
json = sb.toString();
catch (Exception e)
Log.e("Buffer Error", "Error converting result " + e.toString());
// try parse the string to a JSON object
try
jObj = new JSONArray(json);
catch (JSONException e)
Log.e("JSON Parser", "Error parsing data " + e.toString());
// return JSON String
return jObj;
【问题讨论】:
【参考方案1】:在片段中使用 AsyncTask 的步骤很简单,如下所示
将代码放在activityCreateview
new GetJsonFromUrlTask(getActivity(), mListView).execute("");
getActivity()
是将您的活动上下文放入 Fragment 类的方法
注意:您从网站上找到代码,此代码是 AsyncTask 类,您可以将此代码调用到
Activity
,Fragment
,也可以调用任何其他类
更改代码
public class GetJsonFromUrlTask extends AsyncTask<Void, Void, String>
private Context activity;
private String url;
private ProgressDialog dialog;
private final static String TAG = GetJsonFromUrlTask.class.getSimpleName();
public GetJsonFromUrlTask(Context activity, String url)
super();
this.activity = activity;
this.url = url;
@Override
protected void onPreExecute()
super.onPreExecute();
// Create a progress dialog
dialog = new ProgressDialog(activity);
// Set progress dialog title
dialog.setTitle("Getting JSON DATA");
// Set progress dialog message
dialog.setMessage("Loading...");
dialog.setIndeterminate(false);
// Show progress dialog
dialog.show();
@Override
protected String doInBackground(Void... params)
// call load JSON from url method
return loadJSON(this.url).toString();
@Override
protected void onPostExecute(String result)
dialog.dismiss();
Log.i(TAG, result);
public JSONArray loadJSON(String url)
// Creating JSON Parser instance
JSONGetter jParser = new JSONGetter();
// getting JSON string from URL
JSONArray json = jParser.getJSONFromUrl(url);
return json;
private class JSONGetter
private InputStream is = null;
private JSONArray jObj = null;
private String json = "";
// constructor
public JSONGetter()
public JSONArray getJSONFromUrl(String url)
// Making HTTP request
try
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
catch (UnsupportedEncodingException e)
e.printStackTrace();
catch (ClientProtocolException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
try
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"),
8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
sb.append(line + "\n");
is.close();
json = sb.toString();
catch (Exception e)
Log.e("Buffer Error", "Error converting result " + e.toString());
// try parse the string to a JSON object
try
jObj = new JSONArray(json);
catch (JSONException e)
Log.e("JSON Parser", "Error parsing data " + e.toString());
// return JSON String
return jObj;
【讨论】:
还是不行。它说了一些关于空指针的事情。我认为是因为没有调用 parseJsonResponse 方法以上是关于如何在片段中使用 GetJsonFromUrlTask.java的主要内容,如果未能解决你的问题,请参考以下文章