この質問が 100 回聞かれることは知っていますが、何らかの理由でコードの何が問題なのかを見つけることができません。私はAndroidとJavaが初めてです。基本的に、私は記事を取得したいワードプレスのウェブサイトを持っています。json 配列形式でデータを取得するために wp-rest-api v2 をインストールしました。私のAndroidアプリでは
MainActivity.java
package al.im.imp;
/*ALL THE IMPORTS*/
public class ImpHome extends AppCompatActivity implements View.OnClickListener {
ListView lstTest;
JSONAdapter mJSONAdapter;
Button search_Button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imp_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
//Access Listview
lstTest = (ListView) findViewById(R.id.home_list);
search_Button = (Button) findViewById(R.id.button1);
search_Button.setOnClickListener(this);
mJSONAdapter = new JSONAdapter(this, getLayoutInflater());
lstTest.setAdapter(mJSONAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_impakt_home, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void queryImp() {
// Create a client to perform networking
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://example.com/wp-json/wp/v2/posts",
new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONArray jsonArray) {
Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_LONG).show();
mJSONAdapter.updateData(jsonArray);
}
@Override
public void onFailure(int statusCode, Throwable throwable, JSONObject error) {
// Display a "Toast" message
// to announce the failure
Toast.makeText(getApplicationContext(), "Error: " + statusCode + " " + throwable.getMessage(), Toast.LENGTH_LONG).show();
// Log error message
// to help solve any problems
Log.e("Imp android", statusCode + " " + throwable.getMessage());
}
});
}
@Override
public void onClick(View v) {
queryImp();
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onStop() {
super.onStop();
}
}
私の article_list.xml レイアウト
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="75dp">
<TextView
android:id="@+id/text_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"/>
</RelativeLayout>
そして私の JSONAdapter.java クラス
package al.imp.imp;
/*IMPORTS*/
public class JSONAdapter extends BaseAdapter{
Context mContext;
LayoutInflater mInflater;
JSONArray mJsonArray;
public JSONAdapter(Context context, LayoutInflater inflater) {
mContext = context;
mInflater = inflater;
mJsonArray = new JSONArray();
}
@Override
public int getCount() {
return mJsonArray.length();
}
@Override
public Object getItem(int position) {
return mJsonArray.optJSONObject(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.article_list, null);
holder = new ViewHolder();
holder.titleTextView = (TextView) convertView.findViewById(R.id.text_title);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
JSONObject jsonObject = (JSONObject) getItem(position);
String articleTitle = "";
articleTitle = jsonObject.optJSONObject("title").optString("rendered");
holder.titleTextView.setText(articleTitle);
return convertView;
}
private static class ViewHolder {
public TextView titleTextView;
}
public void updateData(JSONArray jsonArray) {
// update the adapter's dataset
mJsonArray = jsonArray;
notifyDataSetChanged();
}
}
なぜこれが機能しないのかわかりません。次のようなデータをログに記録しようとすると
for (int i = 1; i < jsonArray.length(); i++) {
Log.d("Title is:", jsonArray.optJSONObject(i).getJSONObject("title").getString("rendered").toString());
Log.d("Image Url is:", jsonArray.optJSONObject(i).getString("featured_image_thumbnail_url").toString());
}
それは完全に機能するので、json応答を取得するのに問題はないと思いますが、リストビューにデータを入力します。
お願い助けて