Categories Android Studio Tutorial

Show data from mysql database to android fragment

There are two files.
1] java file.
2] fragment file.

1] Java file code
package com.example.clinicmgmt;
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 {
public fetchData() {
}
String data = “”;
String dataParsed = “”;
String singleParsed = “”;
@Override
protected Void doInBackground(Void… voids) {
try {
URL url= new URL(“http://rajeshkumargupta.000webhostapp.com/viewExpenses.php”);
HttpURLConnection 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; i2] Fragment file code
package com.example.clinicmgmt;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

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;
import java.util.ArrayList;

/**
* A simple {@link Fragment} subclass.
*/
public class ViewExpensesFragment extends Fragment {
ListView listView;
public static TextView data;
public ViewExpensesFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_view_expenses, container, false);
data = (TextView) view.findViewById(R.id.fetcheddata);
fetchData process = new fetchData();
process.execute();
return view;
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *