我想這個佈局XML simpe後來http://3pi.tf/test.xml但首先我只是想將它佈置到我的Android應用程序的解析吧..獲取XML頁面到我的Android應用程序
因此,有我的MainActivity.java
public class MainActivity extends Activity {
private static final String TAG = null;
TextView textview1;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v(TAG, "test1");
textview1 = (TextView) findViewById(R.id.textview1);
Log.v(TAG, "test2");
getXmlTask task = new getXmlTask(textview1 , "http://www.3pi.tf/test.xml");
Log.v(TAG, "test3");
task.execute();
}
}
而且還有我的getXmltask.java
public class getXmlTask extends AsyncTask<Void, Void, String>{
private static final String TAG2 = null;
private WeakReference<TextView> textViewReference;
private String url;
public getXmlTask(TextView textview1, String string) {
// TODO Auto-generated constructor stub
}
public void GetXmlTask(TextView textview, String url) {
this.textViewReference = new WeakReference<TextView>(textview);
this.url = url;
}
@Override
protected String doInBackground(Void... params) {
HttpClient hc = new DefaultHttpClient();
Log.v(TAG2, "testnew");
HttpPost post = new HttpPost(url);
Log.v(TAG2, "testurl");
HttpResponse rp = null;
try {
rp = hc.execute(post);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.v(TAG2, "testpost");
if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
try {
return EntityUtils.toString(rp.getEntity());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "Error";
}
@Override
protected void onPostExecute(String result) {
TextView textView = textViewReference.get();
if(textView != null) {
textView.setText(result);
}
}
}
一些logcat的我能找到這裏的「光標無法通過」行...... 所以logcat的「testnew」是好的,但不是logcat「testurl」,所以這意味着這條線
HttpPost post = new HttpPost(url);
不好嗎?有人能幫助我嗎 ? 我只是想將這個XML佈局到我的Android應用程序,如果有人能告訴我它會是多麼棒! ^^
一些更多的信息,應用程序有一個白色的頁面開始,幾秒鐘後,我得到這個彈出消息說,我的應用程序已經停止......
你需要得到xml所以使用'HTTP GET'方法 – Raghunandan
我替換Post來獲取,但我得到了相同的logcat .. Testurl無法通過:/ ^^ – Nemka
你嘗試過我的帖子中的代碼?? – Raghunandan