簡體   English   中英

Android中的wordpress json解析

[英]wordpress json parsing in android

我沒有在Android應用程序中獲取帖子。 這是我的MainActivity源代碼。

我直接在Android設備上運行此應用。 並且在我的設備上啟用了Internet連接。 它只顯示正在加載的帖子,並且發生了什么事情。

public class MainActivity extends Activity {
ListView postList;

ArrayList<String> postArrayList=new ArrayList<String>();
ArrayAdapter<String> postAdapter;
Context context;
String feedUrl="http://www.aurangabadhq.com/?json=get_recent_posts";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context=this;
    postList = (ListView) findViewById(R.id.postList);
    postAdapter = new ArrayAdapter<String>(this,R.layout.post_list_item, postArrayList);
    postList.setAdapter(postAdapter);
    PostListTask loaderTask=new PostListTask();
    loaderTask.execute();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
public class PostListTask extends AsyncTask<Void,Void,Void>{
    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        dialog= new ProgressDialog(context);
        dialog.setTitle("Loading Posts...");
        dialog.show();
        super.onPreExecute();
    }



    @Override
    protected Void doInBackground(Void... params)
    {
        HttpClient client=new DefaultHttpClient();
        HttpGet getRequest=new HttpGet(feedUrl);
        try {
            HttpResponse response=client.execute(getRequest);
            StatusLine StatusLine= response.getStatusLine();
            int statusCode=StatusLine.getStatusCode();

            if(statusCode !=200 )
            {
                return null;
            }
            InputStream jsonStream=response.getEntity().getContent();
            BufferedReader reader=new BufferedReader(new InputStreamReader(jsonStream));
            StringBuilder builder=new StringBuilder();
            String line;
            while((line=reader.readLine())!=null)
            {
                builder.append(line);
            }
            String jsonData=builder.toString();
            JSONObject json=new JSONObject(jsonData);
            JSONObject object=new JSONObject("object");
            JSONArray posts=new JSONArray("posts");
            for(int i=0;i<posts.length();i++)
            {
                JSONObject post=posts.getJSONObject(i);

                postArrayList.add(post.getString("title"));
            }



        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        dialog.dismiss();
        postAdapter.notifyDataSetChanged();
        super.onPostExecute(aVoid);
    }

}

@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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

您可以從網絡上獲得Json嗎?

嘗試這個:

JSONObject json=new JSONObject(jsonData);
JSONArray posts = json.getJSONArray("posts");
for(int i = 0; i < posts.length(); i++) {
     JSONObject post=posts.getJSONObject(i);
     postArrayList.add(post.getString("title"));
}

不知道這是否行得通,因為我沒有檢查過您的json。 我建議嘗試記錄jsonData字符串,以查看您是否甚至從網絡獲取json。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM