Fetching JSON simple form



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".json1.JSONActivity">
<Button
android:id="@+id/btn_json"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="JSON fetch" />
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="fetch" />
</LinearLayout>
package com.arham.csdevbin.all_android_concept.json1;
import android.app.ProgressDialog;
import android.content.Context;
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 FetchJson extends AsyncTask<Void, Void, Void> {
String data = "";
ProgressDialog progressBar;
Context context;
String dataParsed = "";
String singleParsed = "";
public FetchJson(Context context) {
this.context = context;
this.progressBar = new ProgressDialog(context);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.show();
}
@Override
protected Void doInBackground(Void... voids) {
try {
URL url = new URL("https://api.myjson.com/bins/q3k94");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream inputStream = connection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while (line != null) {
line = bufferedReader.readLine();
data = data + line;
}
JSONArray jsonArray = new JSONArray(data);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
singleParsed = "Name: " + jsonObject.get("name") + "\n" +
"password: " + jsonObject.get("password") + "\n" +
"contact: " + jsonObject.get("contact") + "\n" +
"country: " + jsonObject.get("country") + "\n";
dataParsed = dataParsed + singleParsed + "\n";
}
} 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);
JSONActivity.showJSON.setText(this.dataParsed);
progressBar.dismiss();
}
}
view raw FetchJson.java hosted with ❤ by GitHub
package com.arham.csdevbin.all_android_concept.json1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.arham.csdevbin.all_android_concept.MainActivity;
import com.arham.csdevbin.all_android_concept.R;
public class JSONActivity extends AppCompatActivity {
Button click;
public static TextView showJSON;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json);
click = findViewById(R.id.btn_json);
showJSON = findViewById(R.id.txt);
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FetchJson fetchJson = new FetchJson(JSONActivity.this);
fetchJson.execute();
}
});
}
}

Comments :

Post a Comment