简体   繁体   English

在我的Android APP上布置XML网站

[英]layout an XML website on my Android APP

I've been searching for a long time to answer to my simple question but haven't found it yet. 我一直在寻找很长时间来回答我的简单问题,但尚未找到答案。

I've just started Android Development and I can't manage to layout this simple XML to the Android App I have just created. 我刚刚开始Android开发,因此无法将这个简单的XML布局到我刚创建的Android应用程序中。

There is my code : 有我的代码:

  public class MainActivity extends Activity {
      private static final String TAG = null;

    /** Called when the activity is first created. */
      private String getPage() {
            String str = null ;
            Log.v(TAG, "testentreemethode");

            try
            {
                HttpClient hc = new DefaultHttpClient();
                Log.v(TAG, "testnew");
                HttpPost post = new HttpPost("http://www.3pi.tf/test.xml");
                Log.v(TAG, "testurl");
                HttpResponse rp = hc.execute(post);
                Log.v(TAG, "testpost");

                if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
                {
                    str = EntityUtils.toString(rp.getEntity());
                }
            }catch(IOException e){
                e.printStackTrace();
            }  

            return str;
        }

@Override
public void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  TextView txt = (TextView) findViewById(R.id.textview1);
  Log.v(TAG, "test1");
  txt.setText(getPage());
  Log.v(TAG, "test2");
 }
} 

As you can see I put some Logcat to see where the "cursor" goes and it can't pass this line: 如您所见,我放置了一些Logcat来查看“光标”的位置,并且它无法通过以下行:

HttpResponse rp = hc.execute(post);

Can someone help me please? 有人能帮助我吗?

Network operation cannot be performed on the main thread. 无法在主线程上执行网络操作。 Use an AsyncTask to execute it on a seperate thread like this: 使用AsyncTask在单独的线程上执行它,如下所示:

public class GetXmlTask extends AsyncTask<Void, Void, String> {

    // WeakReferences are used to prevent memory leaks.
    // Always use WeakReferences when referencing Views or Activities or a Context from a seperate thread
    private final WeakReference<TextView> textViewReference;
    private final String url;

    public 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(TAG, "testnew");
        HttpPost post = new HttpPost(url);
        Log.v(TAG, "testurl");
        HttpResponse rp = hc.execute(post);
        Log.v(TAG, "testpost");

        if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
        {
            return EntityUtils.toString(rp.getEntity());
        }
        return "Error";
    }   

    @Override
    protected void onPostExecute(String result) {       
        TextView textView = textViewReference.get();
        if(textView != null) {
            textView.setText(result);
        }       
    }
}

You can execute the task like this: 您可以执行以下任务:

GetXmlTask task = new GetXmlTask(textView, "http://www.3pi.tf/test.xml");
task.execute(); 

In any application you should avoid IO calls on main thread because it is used to handle user events and UI in general. 在任何应用程序中,都应避免在主线程上进行IO调用,因为它通常用于处理用户事件和UI。 in android doing so causes NetworkOnMainThreadException 在Android中这样做会导致NetworkOnMainThreadException

Try to move your web calls to a background thread and it should work. 尝试将您的网络呼叫移至后台线程,它应该可以工作。

ex

public class MainActivity extends Activity {
TextView textView;
Handler mHandler;
  private static final String TAG = null;

/** Called when the activity is first created. */
  private String getPage() {
        String str = null ;
        Log.v(TAG, "testentreemethode");

        try
        {
            HttpClient hc = new DefaultHttpClient();
            Log.v(TAG, "testnew");
            HttpPost post = new HttpPost("http://www.3pi.tf/test.xml");
            Log.v(TAG, "testurl");
            HttpResponse rp = hc.execute(post);
            Log.v(TAG, "testpost");

            if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
            {
                str = EntityUtils.toString(rp.getEntity());
            }
        }catch(IOException e){
            e.printStackTrace();
        }  

        return str;
    }

 @Override
public void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 txtView = (TextView) findViewById(R.id.textview1);
 mHandler = new Handler();
 new Thread(){
    @Override
  public void run(){
    final String str = getPage();
    mHandler.post(new Runnable(){
         @Override
      public void run(){
         textView.setText(str);
       }
   });
 }
  }.start();
Log.v(TAG, "test1");
Log.v(TAG, "test2");
}
} 

Please take a look at this tutorial for better understanding of android threadining. 请看一下本教程,以更好地了解android threadining。 tutorial 教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM