Jozef Štefanek
- Forum posts: 2
Nov 18, 2017, 6:31:18 PM via Website
Nov 18, 2017 6:31:18 PM via Website
I am really fighting with this many hours - I just want to get the JSON data from httpurlconnection, but now I have only Connecting server displaying and nothing happens.
This is my whole code:
@SuppressWarnings("deprecation")
public class JSONParsingFragment extends Fragment{
ListView lv;
ArrayList actorsList;
ActorAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View vw=inflater.inflate(R.layout.jsonparse_fragment, container, false);
lv=(ListView) vw.findViewById(R.id.listView1);
actorsList = new ArrayList<Actors>();
new JSONAsynTask().execute("fullpath.../JSONParsingTutorial/jsonActors");
adapter = new ActorAdapter(getActivity(), R.layout.jsonparsedata_item, actorsList);
lv.setAdapter(adapter);
return vw;
}
class JSONAsynTask extends AsyncTask<String, Void, Void> {
String result;
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(getActivity());
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected Void doInBackground(String... strings) {
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(strings[0]);
urlConnection = (HttpURLConnection) url.openConnection();
int responseCode = urlConnection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
JSONObject jsono = new JSONObject();
JSONArray jarray = jsono.getJSONArray("actors");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
Actors actor = new Actors();
actor.setName(object.getString("name"));
actor.setDescription(object.getString("description"));
actor.setDob(object.getString("dob"));
actor.setCountry(object.getString("country"));
actor.setHeight(object.getString("height"));
actor.setSpouse(object.getString("spouse"));
actor.setChildren(object.getString("children"));
actor.setImage(object.getString("image"));
actorsList.add(actor);
Log.v("log", "response");
}
} } catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;}
protected void onPostExecute(Boolean result) {
dialog.dismiss();
adapter.notifyDataSetChanged();
if(result == false)
Toast.makeText(getActivity(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
}
}
So it seems I have an error in this part:
if(responseCode == HttpURLConnection.HTTP_OK){
JSONObject jsono = new JSONObject();
JSONArray jarray = jsono.getJSONArray("actors");
Can you please help me how to get the JSON data?
— modified on Nov 18, 2017, 6:31:41 PM
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.