0
我没有在我的Android应用中获得帖子。这是我的MainActivity的源代码。wordpress json在android中解析
我直接在我的Android设备上运行此应用程序。而且我的设备上也启用了Internet连接。这只能说明载入站和mothing发生.. :(
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);
}
}
Thanx为快速反应..但仍然..它什么也没有显示。请检查我的JSON ..我想代码中没有错误..你可以在这里检查我的JSON,“http://www.aurangabadhq.com/?json=get_recent_posts” – 2014-09-25 18:25:40
所以如果你把Log.d(“YourApp “,jsonData); 只是在声明之后,Logcat中显示了什么? – user3482211 2014-09-25 18:32:47
ddms:无法绑定到本地8600进行调试 ddmlib:建立的连接被主机中的软件中止 java.io.IOException:建立的连接被主机中的软件中止 \t at sun .nio.ch.SocketDispatcher.write0(Native Method) \t at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:51) \t at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93) \t在sun.nio.ch.IOUtil.write(IOUtil.java:65) \t在sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:487) – 2014-09-25 18:52:26