Android 正在尝试查询Facebook个人资料并将其排序到ListView中
我正在使用Facebook Graph API请求Facebook个人资料列表,并在显示姓名和个人资料图片的列表视图中返回它们。我的主要活动中有两个EditText字段,用于输入名字和第二个名字。单击并传递名称时,按钮将启动我的ListViewActivity。ListView膨胀,请求被发送到Facebook,ListView中的每一行都使用自定义适配器进行更新 我怀疑问题在于内存泄漏。这是非常喜怒无常的,如果我幸运的话,它会工作一两次,任何其他更新ListView的尝试都不会工作,活动将保持空白。它也不适用于一些更常见的名称 以下是我的ListViewActivity:Android 正在尝试查询Facebook个人资料并将其排序到ListView中,android,facebook,listview,facebook-graph-api,android-listview,Android,Facebook,Listview,Facebook Graph Api,Android Listview,我正在使用Facebook Graph API请求Facebook个人资料列表,并在显示姓名和个人资料图片的列表视图中返回它们。我的主要活动中有两个EditText字段,用于输入名字和第二个名字。单击并传递名称时,按钮将启动我的ListViewActivity。ListView膨胀,请求被发送到Facebook,ListView中的每一行都使用自定义适配器进行更新 我怀疑问题在于内存泄漏。这是非常喜怒无常的,如果我幸运的话,它会工作一两次,任何其他更新ListView的尝试都不会工作,活动将保持
public class ListViewActivity extends Activity implements OnItemClickListener {
public String firstname, secondname;
URL imageURL;
ArrayList<String> names = new ArrayList<String>();
ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
public static final String[] descriptions = new String[] {"Description"};
ListView listView;
List<RowItem> rowItems;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
loadData();
rowItems = new ArrayList<RowItem>();
listView = (ListView) findViewById(R.id.listview);
CustomListViewAdapter adapter = new CustomListViewAdapter(this,
R.layout.list_item, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
public void loadData() {
Session session = Session.getActiveSession();
firstname = getIntent().getExtras().getString("firstname");
secondname = getIntent().getExtras().getString("secondname");
if(session==null){
session = Session.openActiveSessionFromCache(this);
} else if (session.isOpened()) {
Bundle params = new Bundle();
params.putString("fields", "name, picture, url, id");
params.putString("limit", "10");
Request request = new Request(session, "search?q="+ firstname + "%20" + secondname + "&limit=10&type=user&fields=name,picture", params, HttpMethod.GET, new Request.Callback() {
@Override
public void onCompleted(Response response) {
// TODO Auto-generated method stub
JSONObject jsonObject = null;
JSONArray jArray = null;
imageURL = null;
try {
jsonObject = new JSONObject(response.getGraphObject().getInnerJSONObject().toString());
jArray = jsonObject.getJSONArray("data");
for (int i = 0; i < jArray.length(); i++) {
JSONObject element = null;
element = jArray.getJSONObject(i);
imageURL = new URL("http://graph.facebook.com/"+ element.get("id") +"/picture?type=large");
names.add(element.getString("name").toString());
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
bitmaps.add(BitmapFactory.decodeStream(imageURL.openConnection().getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
thread.start();
thread.join();
if (bitmaps.size() == jArray.length()) {
for (int j=0; j<jArray.length(); j++) {
RowItem item = new RowItem(bitmaps.get(j), names.get(j), descriptions[0]);
rowItems.add(item);
}
}
}
} catch (JSONException e) {
System.out.println("JSON EXCEPTION:"+ e);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
} else {
Log.d("close", " ");
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast toast = Toast.makeText(getApplicationContext(),
"Item " + (position + 1) + ": " + rowItems.get(position),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
公共类ListViewActivity扩展活动实现了ListListener{
公共字符串firstname,secondname;
URL-imageURL;
ArrayList name=新的ArrayList();
ArrayList位图=新的ArrayList();
公共静态最终字符串[]说明=新字符串[]{“说明”};
列表视图列表视图;
列出项目;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
loadData();
rowItems=新的ArrayList();
listView=(listView)findViewById(R.id.listView);
CustomListViewAdapter=新的CustomListViewAdapter(此,
R.layout.list_项目,行项目);
setAdapter(适配器);
setOnItemClickListener(this);
}
公共void loadData(){
Session=Session.getActiveSession();
firstname=getIntent().getExtras().getString(“firstname”);
secondname=getIntent().getExtras().getString(“secondname”);
如果(会话==null){
session=session.openActiveSessionFromCache(this);
}else if(session.isOpened()){
Bundle params=新Bundle();
参数putString(“字段”、“名称、图片、url、id”);
参数putString(“限制”、“10”);
请求=新请求(会话,“search?q=“+firstname+“%20”+secondname+”&limit=10&type=user&fields=name,picture)”,参数,HttpMethod.GET,新请求。回调(){
@凌驾
未完成公共无效(响应){
//TODO自动生成的方法存根
JSONObject JSONObject=null;
JSONArray-jArray=null;
imageURL=null;
试一试{
jsonObject=newJSONObject(response.getGraphObject().getInnerJSONObject().toString());
jArray=jsonObject.getJSONArray(“数据”);
for(int i=0;i 对于(int j=0;j缩放图像是否有帮助
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
我在第67行加了这两行
public class ListViewActivity extends Activity implements OnItemClickListener {
public String firstname, secondname;
URL imageURL;
ArrayList<String> names = new ArrayList<String>();
ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
public static final String[] descriptions = new String[] {"Description"};
ListView listView;
List<RowItem> rowItems;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
loadData();
rowItems = new ArrayList<RowItem>();
listView = (ListView) findViewById(R.id.listview);
CustomListViewAdapter adapter = new CustomListViewAdapter(this,
R.layout.list_item, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
public void loadData() {
Session session = Session.getActiveSession();
firstname = getIntent().getExtras().getString("firstname");
secondname = getIntent().getExtras().getString("secondname");
if(session==null){
session = Session.openActiveSessionFromCache(this);
} else if (session.isOpened()) {
Bundle params = new Bundle();
params.putString("fields", "name, picture, url, id");
params.putString("limit", "10");
Request request = new Request(session, "search?q="+ firstname + "%20" + secondname + "&limit=10&type=user&fields=name,picture", params, HttpMethod.GET, new Request.Callback() {
@Override
public void onCompleted(Response response) {
// TODO Auto-generated method stub
JSONObject jsonObject = null;
JSONArray jArray = null;
imageURL = null;
try {
jsonObject = new JSONObject(response.getGraphObject().getInnerJSONObject().toString());
jArray = jsonObject.getJSONArray("data");
for (int i = 0; i < jArray.length(); i++) {
JSONObject element = null;
element = jArray.getJSONObject(i);
imageURL = new URL("http://graph.facebook.com/"+ element.get("id") +"/picture?type=large");
names.add(element.getString("name").toString());
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
bitmaps.add(BitmapFactory.decodeStream(imageURL.openConnection().getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
thread.start();
thread.join();
if (bitmaps.size() == jArray.length()) {
for (int j=0; j<jArray.length(); j++) {
RowItem item = new RowItem(bitmaps.get(j), names.get(j), descriptions[0]);
rowItems.add(item);
}
}
}
} catch (JSONException e) {
System.out.println("JSON EXCEPTION:"+ e);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
} else {
Log.d("close", " ");
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast toast = Toast.makeText(getApplicationContext(),
"Item " + (position + 1) + ": " + rowItems.get(position),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
}
公共类ListViewActivity扩展活动实现了ListListener{
公共字符串firstname,secondname;
URL-imageURL;
ArrayList name=新的ArrayList();
ArrayList位图=新的ArrayList();
公共静态最终字符串[]说明=新字符串[]{“说明”};
列表视图列表视图;
列出项目;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
loadData();
rowItems=新的ArrayList();
listView=(listView)findViewById(R.id.listView);
CustomListViewAdapter=新的CustomListViewAdapter(此,
R.layout.list_项目,行项目);
setAdapter(适配器);
setOnItemClickListener(this);
}
公共void loadData(){
Session=Session.getActiveSession();
firstname=getIntent().getExtras().getString(“firstname”);
secondname=getIntent().getExtras().getString(“secondname”);
如果(会话==null){
session=session.openActiveSessionFromCache(this);
}else if(session.isOpened()){
Bundle params=新Bundle();
参数putString(“字段”、“名称、图片、url、id”);
参数putString(“限制”、“10”);
请求=新请求(会话,“search?q=“+firstname+“%20”+secondname+”&limit=10&type=user&fields=name,picture)”,参数,HttpMethod.GET,新请求。回调(){
@凌驾
未完成公共无效(响应){
//TODO自动生成的方法存根
JSONObject JSONObject=null;
JSONArray-jArray=null;
imageURL=null;
试一试{
jsonObject=newJSONObject(response.getGraphObject().getInnerJSONObject().toString());
jArray=jsonObject.getJSONArray(“数据”);
for(int i=0;i