Итак, я пытаюсь получить следующий API (сгенерированный с использованием Django -rest-framework), используя Retrofit на моем реальном android устройстве, подключенном через провод.
"followers": [
{
"id": 4,
"user_id": 11,
"username": "aaaa",
"profileImage": "",
"profileTitle": null,
"is_verified": false
},
{
"id": 1,
"user_id": 5,
"username": "eeee",
"profileImage": "",
"profileTitle": "just try",
"is_verified": true
}
]
Но когда я открываю приложение, оно вылетает со следующей ошибкой.
Unfortunately, Dummy has stopped
Ниже мой Java код
Основная активность
package com.example.dummy;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
ImageButton home;
ImageButton profile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
home = (ImageButton)findViewById(R.id.imageButton);
home.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openMainActivity();
}
});
profile = (ImageButton)findViewById(R.id.imageButton2);
profile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openProfile();
}
});
}
private void openProfile() {
Intent intent = new Intent(this,Profile.class);
startActivity(intent);
}
private void openMainActivity() {
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
}
}
Профиль
package com.example.dummy;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class Profile extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
final ListView listView = findViewById(R.id.listview);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Api api = retrofit.create(Api.class);
Call<List<Followers>> call = api.getfollowers();
call.enqueue(new Callback<List<Followers>>() {
@Override
public void onResponse(Call<List<Followers>> call, Response<List<Followers>> response) {
List<Followers> followers = response.body();
String[] names = new String[followers.size()];
for (int i = 0; i < followers.size() ; i++){
names[i] = followers.get(i).getUsername();
}
listView.setAdapter(
new ArrayAdapter<String>(
getApplicationContext(),
android.R.layout.simple_dropdown_item_1line,
names
)
);
}
@Override
public void onFailure(Call<List<Followers>> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage() , Toast.LENGTH_SHORT).show();
}
});
}
}
Подписчики
package com.example.dummy;
import android.widget.ImageView;
import org.w3c.dom.Text;
public class Followers {
private int id;
private int user_id;
private String username;
private String imageurl;
private Text profileTitle;
private boolean is_verified;
public String getImageurl() {
return imageurl;
}
public int getId() {
return id;
}
public int getUser_id() {
return user_id;
}
public String getUsername() {
return username;
}
public Text getProfileTitle() {
return profileTitle;
}
public boolean isIs_verified() {
return is_verified;
}
}
Api
package com.example.dummy;
import android.util.Log;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
public interface Api {
String BASE_URL = "http:// 192.168.43.49:8000/\n";
@GET("followers/")
Call<List<Followers>> getfollowers();
}
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
Что я сделал: -
- запущен Django сервер с помощью команды
py manage.py runserver 192.168.43.49:8000
- В настройке Django
ALLOWED_HOSTS = ['*']
Трассировка стека: -
2020-07-11 13:24:50.851 18610-18610/com.example.pop E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pop, PID: 18610
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.pop/com.example.pop.Profile}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2014)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1677)
at android.app.Activity.startActivityForResult(Activity.java:4651)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)
at android.app.Activity.startActivityForResult(Activity.java:4609)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663)
at android.app.Activity.startActivity(Activity.java:4970)
at android.app.Activity.startActivity(Activity.java:4938)
at com.example.pop.MainActivity.openProfile(MainActivity.java:43)
at com.example.pop.MainActivity.access$100(MainActivity.java:10)
at com.example.pop.MainActivity$2.onClick(MainActivity.java:35)
at android.view.View.performClick(View.java:6612)
at android.view.View.performClickInternal(View.java:6589)
at android.view.View.access$3100(View.java:785)
at android.view.View$PerformClick.run(View.java:25925)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6823)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Помогите мне найти ошибки или любую ошибку в приведенном выше коде