我想显示我使用列表视图从API获取的联系人。但是当我要运行它时,它被强制关闭。当我尝试查看我创建的日志时,响应之外的usernamefriendprofile数组为null。否则,usernamefriendprofile数组的响应中将包含一个值。如果我将阵列适配器作为响应,则会收到类似
的错误无法解析构造函数'ArrayAdapter(匿名com.androidnetworking.interfaces JSONObject RequestListener,int,int,java.util.ArrayList)
kind: Pod
metadata:
name: gradlecommandfromcommandline
labels:
purpose: gradlecommandfromcommandline
spec:
volumes:
- name: docker-sock
hostPath:
path: /home/vagrant/k8s/pods/gatling/user-files/simulations # A file or
directory location on the node that you want to mount into the Pod
# command: [ "git clone https://github.com/TarunKDas2k18/PerfGatl.git" ]
containers:
- name: gradlecommandfromcommandline
image: tarunkumard/tarungatlingscript:v1.0
workingDir: /opt/gatling-fundamentals/
command: ["./gradlew"]
args: ["gatlingRun-simulations.RuntimeParameters", "-DUSERS=500", "-
DRAMP_DURATION=5", "-DDURATION=30"]
- name: gatlingperftool
image: tarunkumard/gatling:FirstScript # Run the ubuntu 16.04
command: [ "/bin/bash", "-c", "--" ] # You need to run some task inside a
container to keep it running
args: [ "while true; do sleep 10; done;" ] # Our simple program just sleeps inside
an infinite loop
volumeMounts:
- mountPath: /opt/gatling/user-files/simulations # The mount path within the
container
name: docker-sock # Name must match the hostPath volume name
ports:
- containerPort: 80
有人知道如何解决吗?
答案 0 :(得分:1)
@Josep所说的更改此内容
JSONObject obj = new JSONObject(String.valueOf(response));
至:
JSONObject obj = new JSONObject(response.toString());
并移动此块:
arrayAdapter = new ArrayAdapter<String>(this,R.layout.activity_listview,R.id.usernameTeman,usernameFriendsProfile);
teman.setAdapter(arrayAdapter);
到onResponse
方法的结尾。
您正在异步获取数据,因此onResponse
中的代码将在适配器初始化后执行。
答案 1 :(得分:0)
尝试稍微更改类变量:
ArrayList<String> usernameFriendsProfile = null;
ArrayAdapter<String> arrayAdapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tokenUser= getIntent().getStringExtra("token");
Log.d(TAG, "tokenmainactivity" + tokenUser); //untuk log pada onerror
Log.d(TAG, "tokenhalamanselanjutnya " + tokenUser);
username = (TextView) findViewById (R.id.user);
usernameFriends = (TextView)findViewById(R.id.usernameTeman);
teman=(ListView)findViewById(R.id.teman);
// initialize the ArrayList here
usernameFriendsProfile = new ArrayList<String>();
arrayAdapter = new ArrayAdapter<String>(this,R.layout.activity_listview,R.id.usernameTeman,usernameFriendsProfile);
teman.setAdapter(arrayAdapter);
initData();
Log.d(TAG, "usernametemendiluar : " + usernameFriendsProfile);
}
我将Adapter
设为类变量。但是,您还可以采用其他方法。
然后在响应中通知Adapter
数据已更新:
public void onResponse(JSONObject response){
Log.d(TAG, "onResponse: " + response); //untuk log pada onresponse
try {
String usernameAkun = response.getString("username");
username.setText(usernameAkun);
Log.d(TAG, "namaprofil : " + username);
JSONArray friends = response.getJSONArray("friends");
for (int i=0;i<friends.length();i++){
JSONObject objek = friends.getJSONObject(i);
usernameFriendsProfile.add(objek.getString("username"));
}
// notify the adapter that the data has been updated
arrayAdapter.notifyDataSetChanged();
Log.d(TAG, "usernametemendidalem : " + usernameFriendsProfile);
} catch (JSONException e) {
e.printStackTrace();
}
}
答案 2 :(得分:0)
您应该使用
JSONObject obj = new JSONObject(response.toString());
代替
JSONObject obj = new JSONObject(String.valueOf(response));
在创建JSONObject时。