I have a Django version that is installed on my laptop and I would like to connect the API to my android app.
I have created a virtual environment too.
I am not able to connect to my Django API, so please help me with how to connect to API. Thanks in advance.
I surfed on the internet but I couldn't get any output.I am posting questions herein for the first time sorry for any mistakes done.
I want to connect the Django API to my project.i want the information to be seen in json form in mobile app.
when I connect my Django API app made in android studio, It shows an error, I have attached a file above.
I am attaching my android file.
I have used a retrofit library. And I want to connect the API to my app.
MainActivity.java
ListView listView;
GridView gridView;
public String cap;
public String cap1;
String[] pid;
String[] ptitle;
String[] pdes;
String[] dt;
String[] lk;
TextView name,descrip;
int capnum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Retrofit retrofit = new Retrofit.Builder().baseUrl(djangoApi.BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
djangoApi api = retrofit.create(djangoApi.class);
name= findViewById(R.id.text);
descrip=findViewById(R.id.image);
Call<List<PostTable>> call = api.gettabledata();
call.enqueue(new Callback<List<PostTable>>() {
@Override
public void onResponse(Call<List<PostTable>> call, Response<List<PostTable>> response) {
List<PostTable> pt = response.body();
assert pt != null;
String[] pid = new String[pt.size()];
String[] ptitle = new String[pt.size()];
String[] pdes = new String[pt.size()];
String[] pdt = new String[pt.size()];
String[] plk = new String[pt.size()];
for (int i = 0; i < pt.size(); i++) {
pid[i] = pt.get(i).getid();
ptitle[i] = pt.get(i).gettitle();
pdes[i] = pt.get(i).getdescription();
pdt[i] = pt.get(i).getdatetime();
plk[i] = pt.get(i).getlikes();
}
for (int i = 0; i < pt.size(); i++) {
if (ptitle[i].equalsIgnoreCase("drhsrh")) {
capnum = i;
cap = pdes[capnum];
cap1=ptitle[capnum];
}
}
name.setText(cap1);
descrip.setText(cap);
//Glide.with(getApplicationContext()).load(cap).into(imageView);
//name.setText(cap1);
}
@Override
public void onFailure(Call<List<PostTable>> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
this is the code for Interface class
interface djangoApi {
String BASE_URL="http://127.0.0.1:8000/";
@GET("post_table/?format=json")
Call<List<PostTable>> gettabledata();
Starting with Android 9 (API level 28), cleartext support is disabled by default.
Please check this documentation.
https://developer.android.com/training/articles/security-config#CleartextTrafficPermitted
If you want to test in local, please add this android:usesCleartextTraffic="true" in the application tag, of the AndroidManifest.xml.