Reputation: 1
I have my own application that I programmed using java in Android studio The application connects to my server and the server returns json data The application takes this information, transforms it, and adds it to... LinearLayout It may be android:orientation="horizontal" or android:orientation="vertical"
Use okHTTP I am connected to my server, but what I notice is that when I connect, my application becomes lag heavy And when inflate Layout like
View singlechannel = getLayoutInflater().inflate(R.layout.channel, null, false);
The application becomes inoperable unless the period for adding items expires Please see the problem I have to understand me
https://youtu.be/EouuWjnEmUQ?si=NsIBgMswG8oim_uI
my Scripts
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
runnable = new Runnable() {
@Override
public void run() {
getLiveMatch();
}
};
handler.postDelayed(runnable, 1000);
}
public void getLiveMatch(){
try {
JSONObject channels = new JSONObject(new HomeClass.getHomeClass().execute(host + "/apiv2/matchlivenow/").get());
JSONArray js = channels.getJSONArray("data");
// Remove PlaceHolder
// homeMatchLiveLinearLayout.removeAllViews();
for (int i = 0; i < 3; i++) {
int x = i;
runnable = new Runnable() {
@Override
public void run() {
homeMatchLiveLinearLayout.getChildAt(x).setVisibility(View.GONE);
}
};
handler.postDelayed(runnable,i*1000);
}
ShimmerFrameLayout shimmerFrameLayout = new ShimmerFrameLayout(this);
for (int i = 0; i < js.length(); i++) {
int x = i;
runnable = new Runnable() {
@Override
public void run() {
try {
JSONObject data = js.getJSONObject(x);
View singlematchlivenow = getLayoutInflater().inflate(R.layout.singlematchlivenow, null, false);
TextView singlematchlivenowfirst = singlematchlivenow.findViewById(R.id.singlematchlivenowfirst);
TextView singlematchlivenowsecond = singlematchlivenow.findViewById(R.id.singlematchlivenowsecond);
TextView singlematchlivenowfirstgool = singlematchlivenow.findViewById(R.id.singlematchlivenowfirstgool);
TextView singlematchlivenowsecondgool = singlematchlivenow.findViewById(R.id.singlematchlivenowsecondgool);
TextView singlematchlivenowchannel = singlematchlivenow.findViewById(R.id.singlematchlivenowchannel);
ImageView singlematchlivenowbgimg = singlematchlivenow.findViewById(R.id.singlematchlivenowbgimg);
ImageView singlematchlivenowfirstimg = singlematchlivenow.findViewById(R.id.singlematchlivenowfirstimg);
ImageView singlematchlivenowsecondimg = singlematchlivenow.findViewById(R.id.singlematchlivenowsecondimg);
singlematchlivenowfirst.setText(data.getString("first"));
singlematchlivenowsecond.setText(data.getString("second"));
singlematchlivenowfirstgool.setText(data.getString("firstgool"));
singlematchlivenowsecondgool.setText(data.getString("secondgool"));
singlematchlivenowchannel.setText(data.getString("channel"));
downloadHomeImage(data.getString("firstimg"), singlematchlivenowfirstimg, shimmerFrameLayout);
downloadHomeImage(data.getString("secondimg"), singlematchlivenowsecondimg, shimmerFrameLayout);
downloadHomeImage(data.getString("bgimg"), singlematchlivenowbgimg, shimmerFrameLayout);
homeMatchLiveLinearLayout.addView(singlematchlivenow);
}catch (Exception e){
e.printStackTrace();
}
}
};
handler.postDelayed(runnable, x*100);
}
} catch (Exception e) {
e.printStackTrace();
}
}
HomeClass.java
import android.os.AsyncTask;
import android.util.Log;
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class HomeClass {
String host = "http://example.com/json";
public static class getHomeClass extends AsyncTask<String,Void,String> {
Response response;
String cookie = "";
@Override
protected String doInBackground(String... String) {
OkHttpClient client = new OkHttpClient();
String url = String[0];
if (String.length == 2)
{
cookie = String[1];
Log.d("COOKIE2",cookie);
}
Request request = new Request.Builder()
.url(url)
.header("Cookie", cookie)
.header("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0")
.build();
try {
response = client.newCall(request).execute();
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}
Is there a way to add items easily without any lag here? In my application, because I notice that applications add items easily without delay or lag in the application
Upvotes: 0
Views: 20