Теперь приложение умеет получать и парсить ответы от сервера. Недоделанный TreeView для первого уровня ветви.
This commit is contained in:
parent
9983b3cd1c
commit
b9b3bc45ad
@ -1,7 +1,12 @@
|
|||||||
package ru.volgorobot.vrcatalog;
|
package ru.volgorobot.vrcatalog;
|
||||||
|
|
||||||
|
import android.app.Fragment;
|
||||||
|
import android.app.FragmentManager;
|
||||||
|
import android.app.FragmentTransaction;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.preference.PreferenceManager;
|
||||||
import android.support.design.widget.NavigationView;
|
import android.support.design.widget.NavigationView;
|
||||||
import android.support.v4.view.GravityCompat;
|
import android.support.v4.view.GravityCompat;
|
||||||
import android.support.v4.widget.DrawerLayout;
|
import android.support.v4.widget.DrawerLayout;
|
||||||
@ -30,6 +35,17 @@ public class MainActivity extends AppCompatActivity
|
|||||||
|
|
||||||
NavigationView navigationView = findViewById(R.id.nav_view);
|
NavigationView navigationView = findViewById(R.id.nav_view);
|
||||||
navigationView.setNavigationItemSelectedListener(this);
|
navigationView.setNavigationItemSelectedListener(this);
|
||||||
|
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
||||||
|
String baseURL = sharedPreferences.getString("addressOfServer", "");
|
||||||
|
|
||||||
|
FragmentManager fragmentManager = getFragmentManager();
|
||||||
|
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
|
||||||
|
TreeViewFragment treeViewFragment = new TreeViewFragment();
|
||||||
|
|
||||||
|
Bundle bundle = new Bundle();
|
||||||
|
bundle.putString("baseURL", baseURL);
|
||||||
|
treeViewFragment.setArguments(bundle);
|
||||||
|
fragmentTransaction.add(R.id.fragment_container, treeViewFragment).commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -3,16 +3,83 @@ package ru.volgorobot.vrcatalog;
|
|||||||
import android.app.Fragment;
|
import android.app.Fragment;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
import com.unnamed.b.atv.model.TreeNode;
|
import com.unnamed.b.atv.model.TreeNode;
|
||||||
import com.unnamed.b.atv.view.AndroidTreeView;
|
import com.unnamed.b.atv.view.AndroidTreeView;
|
||||||
|
import com.unnamed.b.atv.view.TreeNodeWrapperView;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import retrofit2.Call;
|
||||||
|
import retrofit2.Callback;
|
||||||
|
import retrofit2.Response;
|
||||||
|
import ru.volgorobot.vrcatalog.api.Controller;
|
||||||
|
import ru.volgorobot.vrcatalog.api.VRApi;
|
||||||
|
import ru.volgorobot.vrcatalog.model.FirstLevelModel;
|
||||||
|
|
||||||
public class TreeViewFragment extends Fragment {
|
public class TreeViewFragment extends Fragment {
|
||||||
|
|
||||||
private AndroidTreeView treeView;
|
private AndroidTreeView treeView;
|
||||||
|
private VRApi vrApi;
|
||||||
|
private List<FirstLevelModel> firstLevelObjects = new ArrayList<>();
|
||||||
|
private List<TreeNode> firstLevelNodes = new ArrayList<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Bundle bundle = this.getArguments();
|
||||||
|
String baseURL = bundle.getString("baseURL");
|
||||||
|
vrApi = Controller.getApi(baseURL, getActivity());
|
||||||
|
|
||||||
|
vrApi.getFirstLevel().enqueue(new Callback<List<FirstLevelModel>>() {
|
||||||
|
@Override
|
||||||
|
public void onResponse(Call<List<FirstLevelModel>> call, Response<List<FirstLevelModel>> response) {
|
||||||
|
if(response != null) {
|
||||||
|
try {
|
||||||
|
firstLevelObjects.addAll(response.body());
|
||||||
|
} catch(Exception e) {
|
||||||
|
Log.e("ERROR", "Answer of server is wrong!");
|
||||||
|
Log.e("ERROR", response.message());
|
||||||
|
}
|
||||||
|
fillFirstLevel();
|
||||||
|
} else {
|
||||||
|
Log.e("ERROR", "Что-то пошло не так...");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(Call<List<FirstLevelModel>> call, Throwable t) {
|
||||||
|
Log.e("ERROR", "getFirstLevel() failed!");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
|
||||||
|
View rootView = inflater.inflate(R.layout.fragment_default, null, false);
|
||||||
|
TreeNodeWrapperView containerView = rootView.findViewById(R.id.treeViewContainer);
|
||||||
|
TreeNode root = TreeNode.root();
|
||||||
|
root.addChildren(firstLevelNodes);
|
||||||
|
treeView = new AndroidTreeView(getActivity(), root);
|
||||||
|
//containerView.insertNodeView(treeView);
|
||||||
|
return rootView;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fillFirstLevel() {
|
||||||
|
for (int i = 0; i < firstLevelObjects.size(); i++) {
|
||||||
|
firstLevelNodes.add(new TreeNode(firstLevelObjects.get(i).getName()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,40 @@
|
|||||||
|
package ru.volgorobot.vrcatalog.api;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
|
|
||||||
|
import retrofit2.Retrofit;
|
||||||
|
import retrofit2.converter.gson.GsonConverterFactory;
|
||||||
|
|
||||||
|
public class Controller {
|
||||||
|
private static String baseURL;
|
||||||
|
private static Context context;
|
||||||
|
private static Retrofit retrofit;
|
||||||
|
|
||||||
|
|
||||||
|
public static VRApi getApi(String baseURL1, Context context1) {
|
||||||
|
baseURL = baseURL1;
|
||||||
|
context = context1;
|
||||||
|
Gson gson = new GsonBuilder()
|
||||||
|
.setLenient()
|
||||||
|
.create();
|
||||||
|
|
||||||
|
buildRetrofitObject(gson);
|
||||||
|
|
||||||
|
return retrofit.create(VRApi.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void buildRetrofitObject(Gson gson) {
|
||||||
|
try {
|
||||||
|
retrofit = new Retrofit.Builder()
|
||||||
|
.baseUrl(baseURL)
|
||||||
|
.addConverterFactory(GsonConverterFactory.create(gson))
|
||||||
|
.build();
|
||||||
|
} catch(Exception e) {
|
||||||
|
Toast.makeText(context, "Вы ввели неверный URL. Пример: http://example", Toast.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
13
app/src/main/java/ru/volgorobot/vrcatalog/api/VRApi.java
Normal file
13
app/src/main/java/ru/volgorobot/vrcatalog/api/VRApi.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package ru.volgorobot.vrcatalog.api;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import retrofit2.Call;
|
||||||
|
import retrofit2.http.GET;
|
||||||
|
import ru.volgorobot.vrcatalog.model.FirstLevelModel;
|
||||||
|
|
||||||
|
public interface VRApi {
|
||||||
|
|
||||||
|
@GET("/API/Api.php?action=getFirstLevel")
|
||||||
|
Call<List<FirstLevelModel>> getFirstLevel();
|
||||||
|
}
|
@ -8,4 +8,9 @@
|
|||||||
tools:context=".MainActivity"
|
tools:context=".MainActivity"
|
||||||
tools:showIn="@layout/app_bar_main">
|
tools:showIn="@layout/app_bar_main">
|
||||||
|
|
||||||
|
<RelativeLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:id="@+id/fragment_container"></RelativeLayout>
|
||||||
|
|
||||||
</android.support.constraint.ConstraintLayout>
|
</android.support.constraint.ConstraintLayout>
|
@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<android.support.v7.widget.LinearLayoutCompat
|
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent">
|
android:layout_height="match_parent"
|
||||||
|
android:id="@+id/treeViewContainer">
|
||||||
</android.support.v7.widget.LinearLayoutCompat>
|
</FrameLayout>
|
@ -3,5 +3,6 @@
|
|||||||
<EditTextPreference
|
<EditTextPreference
|
||||||
android:key="addressOfServer"
|
android:key="addressOfServer"
|
||||||
android:title="Адрес PHP скрипта-посредника"
|
android:title="Адрес PHP скрипта-посредника"
|
||||||
android:summary="PHP скрипт-посредник необходим для доступа к БД деталей" />
|
android:summary="PHP скрипт-посредник необходим для доступа к БД деталей"
|
||||||
|
android:defaultValue="http://example.com"/>
|
||||||
</PreferenceScreen>
|
</PreferenceScreen>
|
Loading…
Reference in New Issue
Block a user