2013-12-11 13 views
0

我想這個佈局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應用程序,如果有人能告訴我它會是多麼棒! ^^

一些更多的信息,應用程序有一個白色的頁面開始,幾秒鐘後,我得到這個彈出消息說,我的應用程序已經停止......

+0

你需要得到xml所以使用'HTTP GET'方法 – Raghunandan

+0

我替換Post來獲取,但我得到了相同的logcat .. Testurl無法通過:/ ^^ – Nemka

+0

你嘗試過我的帖子中的代碼?? – Raghunandan

回答

0

使用下面doInbackground

String _respons; 
@Override 
protected String doInBackground(Void... sUrl) { 
try 
    { 
    HttpClient httpclient = new DefaultHttpClient(); 
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); 
    HttpGet request = new HttpGet("http://3pi.tf/test.xml"); 
    HttpResponse response = httpclient.execute(request); 
    HttpEntity resEntity = response.getEntity(); 
    _respons=EntityUtils.toString(resEntity); 
    Log.i(".........",""+_respons); 
    }catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
    return _respons; 

} 
+0

現在的應用程序不會崩潰,但他不佈局xml http://3pi.tf/test.xml ..:/方式非常感謝您的幫助 – Nemka

+0

@Nemka我不明白您的意見。沒有佈局。它是一個xml文件,就是你在'String _respons = EntityUtils.toString(resEntity)中獲得的內容;'你對layout和xml的理解是非常錯誤的。記錄該響應並檢查。 – Raghunandan

+0

其實我只是想讓我的應用程序顯示XML http://3pi.tf/test.xml ^^是否有可能?稍後解析它,因爲我想稍後用XMLParser提取關於這個xml的一些信息。 – Nemka

相關問題