gpt4 book ai didi

java - 将 Facebook 帖子 ID 从 String 转换为 int

转载 作者:行者123 更新时间:2023-12-02 12:57:06 25 4
gpt4 key购买 nike

我正在制作一个新闻源,我可以从特定的 Facebook 页面检索 Facebook 帖子。我在 Facebook Graph API 的帮助下检索这些帖子。我有一个 FeedItem,它有一个 ID (int)。该 ID 还用于检查哪个项目位于当前位置 (Recyclerview)。

问题在于 Facebook 为帖子提供了一个字符串 ID。我不知道如何转换它,以便它可以与我的应用程序一起使用。

我的适配器:

public class FeedListAdapter extends RecyclerView.Adapter<FeedListAdapter.ViewHolder> {

private ImageLoader imageLoader = AppController.getInstance().getImageLoader();
private List<FeedItem> mFeedItems;
private Context mContext;

public FeedListAdapter(List<FeedItem> pFeedItems, Context pContext) {
this.mFeedItems = pFeedItems;
this.mContext = pContext;
}

/* Create methods for further adapter use.*/
@Override
public ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
View feedView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.feed_item, parent, false);
return new ViewHolder(feedView);
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.populateRow(getFeedItem(position));
}

@Override
public long getItemId(int position) {
return mFeedItems.get(position).getId();
}

@Override
public int getItemCount() {
return mFeedItems.size();
}

private FeedItem getFeedItem(int position) {
return mFeedItems.get(position);
}

class ViewHolder extends RecyclerView.ViewHolder implements OnClickListener {

private ImageView mProfilePic;
private TextView mName;
private TextView mTimestamp;
private TextView mTxtStatusMsg;
private FeedImageView mFeedImage;

//initialize the variables
ViewHolder(View view) {
super(view);
mProfilePic = (ImageView) view.findViewById(R.id.feedProfilePic);
mName = (TextView) view.findViewById(R.id.feedName);
mTimestamp = (TextView) view.findViewById(R.id.feedTimestamp);
mTxtStatusMsg = (TextView) view.findViewById(R.id.feedStatusMessage);
mFeedImage = (FeedImageView) view.findViewById(R.id.feedImage);
view.setOnClickListener(this);
}

@Override
public void onClick(View view) {

}

private void populateRow(FeedItem pFeedItem) {
getProfilePic(pFeedItem);

mName.setText(pFeedItem.getName());

mTimestamp.setText(pFeedItem.getTimeStamp());

mTxtStatusMsg.setText(pFeedItem.getStatus());

getStatusImg(pFeedItem);
}

private void getProfilePic(FeedItem pFeedItem) {
imageLoader.get(pFeedItem.getProfilePic(), new ImageListener() {
@Override
public void onResponse(ImageContainer response, boolean arg1) {
if (response.getBitmap() != null) {
// load image into imageview
mProfilePic.setImageBitmap(response.getBitmap());
}
}

@Override
public void onErrorResponse(final VolleyError pVolleyError) {

}
});
}

private void getStatusImg(FeedItem pFeedItem) {
if (pFeedItem.getImage() != null) {
mFeedImage.setImageUrl(pFeedItem.getImage(), imageLoader);
mFeedImage.setVisibility(View.VISIBLE);
mFeedImage
.setResponseObserver(new FeedImageView.ResponseObserver() {
@Override
public void onError() {
}

@Override
public void onSuccess() {
}
});
} else {
mFeedImage.setVisibility(View.GONE);
}
}
}

我的 FeedFragment:

public class FeedFragment extends android.support.v4.app.Fragment {

private static final String TAG = FeedFragment.class.getSimpleName();
private FeedListAdapter mListAdapter;
private List<FeedItem> mFeedItems;
private RecyclerView mRecyclerView;
private String FACEBOOKURL = "**URL OF MY FB-POSTDATA**";

// newInstance constructor for creating fragment with arguments
public static FeedFragment newInstance() {
FeedFragment fragment = new FeedFragment();
return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout resource file
View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_feed, container, false);

initRecyclerView(view);
initCache();

return view;
}

@Override
public void onStart() {
super.onStart();
}

@Override
public void onResume() {
super.onResume();
}

private void initRecyclerView(View pView) {
mRecyclerView = (RecyclerView) pView.findViewById(R.id.fragment_feed_recyclerview);
LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setHasFixedSize(false);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mRecyclerView.setNestedScrollingEnabled(true);
}

mFeedItems = new ArrayList<>();

mListAdapter = new FeedListAdapter(mFeedItems, getActivity());
mRecyclerView.setAdapter(mListAdapter);
}

private void initCache() {
// We first check for cached request
Cache cache = AppController.getInstance().getRequestQueue().getCache();
Entry entry = cache.get(FACEBOOKURL);
if (entry != null) {
// fetch the data from cache
try {
String data = new String(entry.data, "UTF-8");
try {
parseJsonFeed(new JSONObject(data));
} catch (JSONException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

} else {
// making fresh volley request and getting json
JsonObjectRequest jsonReq = new JsonObjectRequest(Method.GET,
FACEBOOKURL, null, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
VolleyLog.d(TAG, "Response: " + response.toString());
if (response != null) {
parseJsonFeed(response);
}
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});

// Adding request to volley request queue
AppController.getInstance().addToRequestQueue(jsonReq);
}
}
private void parseJsonFeed(JSONObject response) {
try {
JSONArray feedArray = response.getJSONArray("data");

for (int i = 0; i < feedArray.length(); i++) {
JSONObject feedObj = (JSONObject) feedArray.get(i);

FeedItem item = new FeedItem();
item.setId(Integer.parseInt(feedObj.getString("id")));
item.setName("name of page");

// Image might be null sometimes
String image = feedObj.isNull("full_picture") ? null : feedObj
.getString("full_picture");
item.setImage(image);
// Status message might be null sometimes
String status = feedObj.isNull("message") ? null : feedObj
.getString("message");
item.setStatus(status);

item.setProfilePic("**profile picture url**");
item.setTimeStamp(feedObj.getString("created_time"));

mFeedItems.add(item);
}

// notify data changes to list adapter
mListAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
} }
正如我所说;我不知道如何处理这个问题,我想这里有人可能知道如何转换它,这样我就可以使用图形 api 给我的字符串,并将其用作整数。

最佳答案

如果 id 都是数字,您应该能够执行以下操作:int id = Integer.valueOf(facebookId)

如果您有取消评分,您可以尝试以下操作:

public int getIdFromString(String postId) {
String finalId;
while (postId.indexOf("_") > 0) {
finalId = postId.substring(0, postId.indexOf("_"));
postId = finalId.concat(postId.substring(postId.indexOf("_") + 1));
}
return Integer.valueOf(postId);
}

关于java - 将 Facebook 帖子 ID 从 String 转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44398179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com