Question: JSON Data Fetching And Parsing From URL with Android Studio problem. Please help I'm trying to parse the following JSON file but am not able

"JSON Data Fetching And Parsing From URL" with Android Studio problem. Please help I'm trying to parse the following JSON file but am not able to do so: api.myjson.com/bins/ z5x5e Is there anything wrong with my code?

Source Code:

AndroidManifest.xml
 
             
 
 
fetchData.java
 

package com.abhishekpanwar.receivedatajson;

import android.os.AsyncTask;

import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject;

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;

public class fetchData extends AsyncTask { String data =""; String dataParsed = ""; String singleParsed =""; @Override protected Void doInBackground(Void... voids) { try { URL url = new URL("https://api.myjson.com/bins/ z5x5e"); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); InputStream inputStream = httpURLConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String line = ""; while(line != null){ line = bufferedReader.readLine(); data = data + line; }

JSONArray JA = new JSONArray(data); for(int i =0 ;i < JA.length(); i++){ JSONObject JO = (JSONObject) JA.get(i); singleParsed = "Type:" + JO.get("type") + "Title:" + JO.get("title") + "Venue:" + JO.get("venue") + "Location:" + JO.get("location") + "Time:" + JO.get("time") + "Description:" + JO.get("desc") + "More:" + JO.get("more");

dataParsed = dataParsed + singleParsed;

}

} catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); }

return null; }

@Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid);

MainActivity.data.setText(this.dataParsed);

} }

MainActivity.java
 
package com.abhishekpanwar.receivedatajson; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { Button click; public static TextView data; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); click = (Button) findViewById(R.id.button); data = (TextView) findViewById(R.id.fetcheddata); click.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { fetchData process = new fetchData(); process.execute(); } }); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!