android app fatch website data java | Project Structure, coding with full path 24 naught

Posted by

Here’s a basic project structure for an Android app in Java that fetches data from a website. This example uses Retrofit for network requests and Gson for JSON parsing.


Project Structure

MyAndroidApp/
├── app/
│   ├── src/main/
│   │   ├── java/com/example/myandroidapp/
│   │   │   ├── MainActivity.java
│   │   │   ├── api/ApiClient.java
│   │   │   ├── api/ApiService.java
│   │   │   ├── model/WebData.java
│   │   │   ├── adapter/DataAdapter.java
│   │   │   ├── utils/Constants.java
│   │   ├── res/layout/
│   │   │   ├── activity_main.xml
│   │   ├── AndroidManifest.xml
├── build.gradle

1 AndroidManifest.xml

Enable Internet Permission to fetch website data.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myandroidapp">

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:theme="@style/Theme.MyAndroidApp"
        android:label="MyAndroidApp">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

2 ApiService.java (Network Interface)

package com.example.myandroidapp.api;

import com.example.myandroidapp.model.WebData;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;

public interface ApiService {
    @GET("path/to/api/endpoint") // Replace with actual endpoint
    Call<List<WebData>> fetchData();
}

3 ApiClient.java (Retrofit Client)

package com.example.myandroidapp.api;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ApiClient {
    private static final String BASE_URL = "https://example.com/"; // Replace with actual website URL
    private static Retrofit retrofit = null;

    public static Retrofit getClient() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

4 WebData.java (Model Class)

package com.example.myandroidapp.model;

import com.google.gson.annotations.SerializedName;

public class WebData {
    @SerializedName("id")
    private int id;

    @SerializedName("title")
    private String title;

    @SerializedName("description")
    private String description;

    public int getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public String getDescription() {
        return description;
    }
}

5 MainActivity.java (Fetching and Displaying Data)

package com.example.myandroidapp;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.myandroidapp.api.ApiClient;
import com.example.myandroidapp.api.ApiService;
import com.example.myandroidapp.model.WebData;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.textView);
        fetchWebsiteData();
    }

    private void fetchWebsiteData() {
        ApiService apiService = ApiClient.getClient().create(ApiService.class);
        Call<List<WebData>> call = apiService.fetchData();

        call.enqueue(new Callback<List<WebData>>() {
            @Override
            public void onResponse(Call<List<WebData>> call, Response<List<WebData>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    StringBuilder data = new StringBuilder();
                    for (WebData item : response.body()) {
                        data.append("ID: ").append(item.getId()).append("\n")
                            .append("Title: ").append(item.getTitle()).append("\n")
                            .append("Description: ").append(item.getDescription()).append("\n\n");
                    }
                    textView.setText(data.toString());
                }
            }

            @Override
            public void onFailure(Call<List<WebData>> call, Throwable t) {
                Log.e("MainActivity", "Error fetching data", t);
                textView.setText("Failed to fetch data");
            }
        });
    }
}

6 activity_main.xml (UI Layout)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Fetching data..."
        android:textSize="18sp" />
</LinearLayout>

7 Dependencies (build.gradle)

Add Retrofit and Gson to app/build.gradle.

dependencies {
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
}

How It Works:

  1. MainActivity calls fetchWebsiteData() on launch.
  2. ApiClient initializes Retrofit with a base URL.
  3. ApiService defines the @GET request.
  4. WebData class maps JSON response to Java objects.
  5. MainActivity updates the TextView with fetched data.

Now, build & run the app to fetch and display website data!

Would you like any modifications or enhancements?