0

我与 VK Api 一起工作。我想在 ArrayList 中显示我的好友列表。我知道为此我需要解析 jsonobject 并创建一个适配器。

 public class MainActivity extends Activity {
TextView text;
getFriendTask task;
ListView listView1;
VKAccessToken token;
String first_name;
String last_name;
String sex;
String bdate;
String city;
String count;
ArrayList<HashMap<String,String>> myfriend;
Friend adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    VKUIHelper.onCreate(this);
    VKSdk.initialize(listener, "4571964");
    VKSdk.authorize(new String[]{"friends"});


    text=(TextView)findViewById(R.id.txt);
}



public void FriendClick(View view) {
    if(listener!=null) {
         VKRequest request = new VKRequest("friends.get", VKParameters.from(VKApiConst.FIELDS, "first_name, last_name, sex, bdate, city"));
        request.start();

        request.executeWithListener(new VKRequest.VKRequestListener() {

            @Override
            public void onComplete(VKResponse response) {
                super.onComplete(response);

               try{
                JSONObject object=new JSONObject(response.responseString);
                JSONArray array=object.getJSONArray("response");
                for(int j=0; j<array.length(); j++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    object = array.getJSONObject(j);
                    map.put(count, object.getString("count"));
                    map.put(first_name, object.getString("first_name"));
                    map.put(last_name, object.getString("last_name"));
                    map.put(sex, object.getString("sex"));
                    map.put(bdate, object.getString("bdate"));
                    map.put(city, object.getString("city"));

                    myfriend.add(map);
                }
                }catch (Exception e) {
                    e.printStackTrace();
                }




               task=new getFriendTask();
                task.execute();


                Log.i("Are You Ready???", "Response - "+response.responseString);

            }

            @Override
            public void onError(VKError error) {
                super.onError(error);
                Log.i("Are You Ready to Fly???", "EEEror - "+error.errorMessage);

            }

            @Override
            public void attemptFailed(VKRequest request, int attemptNumber, int totalAttempts) {
                super.attemptFailed(request, attemptNumber, totalAttempts);

            }
        });
    }
}

class getFriendTask extends AsyncTask<Void,Void, Void> {
    protected void onPostExecute(Void result){
        super.onPostExecute(result);

      listView1=(ListView)findViewById(R.id.list);
        adapter=new Friend(MainActivity.this, myfriend);

    }
    @Override
    protected Void doInBackground(Void... params) {


        return null;
    }


}






VKSdkListener listener=new VKSdkListener() {
    @Override
    public void onCaptchaError(VKError vkError) {


    }

    @Override
    public void onTokenExpired(VKAccessToken vkAccessToken) {

    }

    @Override
    public void onAccessDenied(VKError vkError) {
        Log.d("Gooooo", " My error - "+vkError.errorMessage);
    }
    public void onReceiveNewToken(VKAccessToken newToken){

        Log.d("Gooooo", "My token - "+newToken.accessToken);

        token=newToken;

    }

};

@Override
protected void onResume() {
    super.onResume();
    VKUIHelper.onResume(this);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    VKUIHelper.onDestroy(this);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    VKUIHelper.onActivityResult(requestCode, resultCode, data);
}

public void onAutoClick(View view) {
}

}

还有我的适配器朋友。

 class Friend extends BaseAdapter{
Context context;
LayoutInflater inflater;
HashMap<String, String> resultp=new HashMap<String, String>();
  ArrayList<HashMap<String, String>> data;
  public Friend(Context context, ArrayList<HashMap<String, String>> myfriend){
 this.context=context;
 data=myfriend; }

@Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    TextView fname;
    TextView lname;
    TextView sx;
    TextView bd;
    TextView ct;

   inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView=inflater.inflate(R.layout.friendlist, parent, false);
    resultp=data.get(position);
    fname=(TextView)itemView.findViewById(R.id.fname);
    lname=(TextView)itemView.findViewById(R.id.lname);
    sx=(TextView)itemView.findViewById(R.id.sx);
    bd=(TextView)itemView.findViewById(R.id.bd);
    ct=(TextView)itemView.findViewById(R.id.ct);


    return itemView;
}}

但 Android Studio 给出了与 JSONArray 一致的错误。我不明白这段代码有什么问题???

    10-21 11:16:11.986  22683-22683/standandroid.ru.vktestapi W/System.err﹕            org.json.JSONException: Value {"count":40,"items": [{"last_name":"Канна","id":10543136,"first_name":"Филип","sex":2,"online":0,"city":{"id":1710959,"title":"Кишинев"}},{"last_name":"Мурашева","id":12567913,"first_name":"Юлия","sex":1,"bdate":"4.4","online":0,"city":{"id":1710959,"title":"Кишинев"}},{"last_name":"Волкова","id":15349233,"first_name":"Алёна","sex":1,"bdate":"31.3","online":0,"city":{"id":1710959,"title":"Кишинев"}},{"last_name":"Голдур","id":15890365,"first_name":"Алиса","sex":1,"bdate":"25.2","online":1,"city":{"id":1710959,"title":"Кишинев"}},{"last_name":"Стурза","id":17445476,"first_name":"Лена","sex":1,"bdate":"6.3.1995","online":0},{"last_name":"Чукля","id":20855237,"first_name":"Никита","sex":2,"online":0},{"last_name":"Чакир","id":22441902,"first_name":"Максим","sex":2,"bdate":"4.1.1994","online":1,"city":{"id":1710959,"title":"Кишинев"}},{"last_name":"Кузьмина","id":23260033,"first_name":"Катя","sex":1,"bdate":"27.6.1994","online":0,"city":{"id":1710959,"title":"Кишинев"}},{"last_name":"Горобец","id":24891252,"first_name":"Арина","sex":1,"bdate":"20.12.1994","online":0,"city":{"id":1710959,"title":"Кишинев"}},{"last_name":"Миколюк","id":33387222,"first_name":"Алёна","sex":1,"bdate":"14.10","online":1,"city":{"id":292,"title":"Одесса"}},{"last_name":"Тросиненко","id":38467274,"first_name":"Андрей","sex":2,"bdate":"19.3","online":0},{"last_name":"Романчук","id":38733273,"first_name":"Миха","sex":1,"bdate":"2.6.1937","online":0,"city":{"id":1710959,"title":"Кишинев"}},{"last_name":"Беженарь","id":39117080,"first_name":"Рома","sex":2,"bdate":"14.10","online":1,"city":{"id":1710959,"title":"Кишинев"}},{"last_name":"Огурцов","id":45171519,"first_name":"Александр","sex":2,"bdate":"22.6.1994","online":0,"city":{"id":1710959,"title":"Кишинев"}},{"last_name":"Драгуцану","id":47147705,"first_name":"Никита","sex":2,"bdate":"28.12","online":0,"city":{"id":1710959,"title":"Кишинев"}},{"last_name":"Канна","id":58606740,"first_name":"Франческа","sex":1,"bdate":"25.1","online":0,"city":{"id":1710959,"title":"Кишинев"}},{"last_name":"Кугут","id":62678153,"first_name":"Лёша","sex":2,"online":0,"city":{"id":1710959,"title":"Кишинев"}},{"last_name":"Трапезникова","id":67485434,"first_name":"Ирина","sex":1,"bdate":"15.7","online":0,"city":{"id":1710959,"title":"Кишинев"}},{"last_name":"Литвинов","id":76249958,"first_name":"Никита","sex":2,"online":0},{"last_name":"Чередниченко","id":78574119,"first_name":"Женя","sex":2,"online":0,"city":{"id":1710959,"title":"Кишинев"}},{"last_name":"Сергунин","id":87816032,"first_name":"Саша","sex":2,"bdate":"28.5.1994","online":0,"city":{"id":1710959,"title":"Кишинев"}},{"last_name":"Барский","id":91235673,"first_name":"Вадим","sex":2,"bdate":"22.6.1994","online":0,"city":{"id":1908479,"title":"Antananarivo"}},{"last_name":"Головко","id":94373404,"first_name":"Никита","sex":2,"bdate":"29.1","online":0,"city":{"id":1710959,"title":"Кишинев"}},{"last_name":"Кицак","id":101047911,"first_name":"Алексей","sex":2,"bdate":"12.5","online":0,"city":{"id":1710959,"title":"Кишинев"}},{"last_name":"Глинянов","id":101208573,"first_name":"Глеб","sex":2,"deactivated":"deleted","online":0},{"last_name":"Руденко","id":108330289,"first_name":"Саша","sex":2,"bdate":"16.6","online":0},{"last_name":"Бугаян","id":129188949,"first_name":"Роман","sex":2,"online":0,"city":{"id":1710959,"title":"Кишинев"}},{"last_name":"Мбала","id":139584680,"first_name":"Кристиан","sex":2,"bdate":"10.6","online":1,"city":{"id":1710959,"title":"Кишинев"}},{"last_name":"Заярная","id":154083054,"first_na
 10-21 11:16:11.986  22683-22683/standandroid.ru.vktestapi W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:100)
 10-21 11:16:11.986  22683-22683/standandroid.ru.vktestapi W/System.err﹕ at org.json.JSONObject.getJSONArray(JSONObject.java:552)
 10-21 11:16:11.986  22683-22683/standandroid.ru.vktestapi W/System.err﹕ at standandroid.ru.vktestapi.MainActivity$1.onComplete(MainActivity.java:85)
 10-21 11:16:11.986  22683-22683/standandroid.ru.vktestapi W/System.err﹕ at com.vk.sdk.api.VKRequest$3.run(VKRequest.java:459)
 10-21 11:16:11.986  22683-22683/standandroid.ru.vktestapi W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:730)
 10-21 11:16:11.986  22683-22683/standandroid.ru.vktestapi W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92)
 10-21 11:16:11.986  22683-22683/standandroid.ru.vktestapi W/System.err﹕ at android.os.Looper.loop(Looper.java:137)
 10-21 11:16:11.986  22683-22683/standandroid.ru.vktestapi W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5137)
 10-21 11:16:11.986  22683-22683/standandroid.ru.vktestapi W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
 10-21 11:16:11.986  22683-22683/standandroid.ru.vktestapi W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
 10-21 11:16:11.986  22683-22683/standandroid.ru.vktestapi W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:756)
 10-21 11:16:11.986  22683-22683/standandroid.ru.vktestapi W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:572)
 10-21 11:16:11.986  22683-22683/standandroid.ru.vktestapi W/System.err﹕ at   miui.dexspy.DexspyInstaller.main(DexspyInstaller.java:171)
 10-21 11:16:11.986  22683-22683/standandroid.ru.vktestapi W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0
 'Instead of using BAseAdapter  you can use ArrayArrayAdapter like :-

     class Friend extends ArrayAdapter<HashMap<String,String>>{
Context context;
LayoutInflater inflater;

  public Friend(Context context, int resource,ArrayList<HashMap<String, String>> myfriend){
super(context, resource, myfriend);
 this.context=context;
  }

@Override
public int getCount() {
    return data.size();
}



@Override
public View getView(int position, View convertView, ViewGroup parent) {
    TextView fname;
    TextView lname;
    TextView sx;
    TextView bd;
    TextView ct;
HashMap<String,String> data=getItem(position);
   inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView=inflater.inflate(R.layout.friendlist, parent, false);

    fname=(TextView)itemView.findViewById(R.id.fname);
    lname=(TextView)itemView.findViewById(R.id.lname);
    sx=(TextView)itemView.findViewById(R.id.sx);
    bd=(TextView)itemView.findViewById(R.id.bd);
    ct=(TextView)itemView.findViewById(R.id.ct);
    fname.setText(data.get("first_name"));
    lname.setText(data.get("last_name"));
    sx.setText(data.get("sex"));
    bd.setText(data.get("bdate"));
    ct.setText(data.get("city"));
    return itemView;
}}' 
于 2014-10-21T09:05:03.600 回答