Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spomenik Monument Database

Authors: Donald Niebyl, FUEL, Damon Murray, Stephen Sorrell

1st Edition

0995745536, 978-0995745537

More Books

Students also viewed these Databases questions