【问题标题】:when i connect django API to my app in android studio it shows error当我在 android studio 中将 django API 连接到我的应用程序时,它显示错误
【发布时间】:2020-01-31 02:19:15
【问题描述】:

我的笔记本电脑上安装了一个 Django 版本,我想将 API 连接到我的 android 应用程序。

我也创建了一个虚拟环境。

我无法连接到我的 Django API,所以请帮助我了解如何连接到 API。提前致谢。

我在互联网上冲浪,但没有得到任何输出。我第一次在此处发布问题,对于所犯的任何错误,我深表歉意。

我想将 Django API 连接到我的项目。我希望在移动应用程序中以 json 形式查看信息。

当我连接我在 android studio 中制作的 Django API 应用程序时,它显示错误,我在上面附加了一个文件。

我正在附加我的 android 文件。

我使用了一个改造库。我想将 API 连接到我的应用。

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();
        }
    });
}
}

这是接口类的代码

interface djangoApi {
    String BASE_URL="http://127.0.0.1:8000/";
    @GET("post_table/?format=json")
    Call<List<PostTable>> gettabledata();

【问题讨论】:

  • 你能清楚你的答案吗
  • 您是如何从应用程序调用服务器的?在“假设”它们是应用程序代码中的一些问题之前,请确保该地址是可访问的。

标签: java android api android-studio django-rest-framework


【解决方案1】:

从 Android 9(API 级别 28)开始,默认禁用明文支持。

请查看此文档。

https://developer.android.com/training/articles/security-config#CleartextTrafficPermitted

如果你想在本地测试,请在AndroidManifest.xml的应用标签中添加这个android:usesCleartextTraffic="true"。

<application
    android:name=".App"
    android:allowBackup="false"
    android:fullBackupContent="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:usesCleartextTraffic="true">

【讨论】:

    猜你喜欢
    • 2020-06-05
    • 1970-01-01
    • 2020-07-30
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多