0

我一直在寻找很长时间来回答我的简单问题,但还没有找到。

我刚刚开始 Android 开发,我无法将这个简单的 XML布局到我刚刚创建的 Android 应用程序中。

有我的代码:

  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");
 }
} 

如您所见,我放了一些 Logcat 来查看“光标”的位置,但它无法通过此行:

HttpResponse rp = hc.execute(post);

有人能帮助我吗?

4

2 回答 2

0

无法在主线程上执行网络操作。使用 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);
        }       
    }
}

您可以像这样执行任务:

GetXmlTask task = new GetXmlTask(textView, "http://www.3pi.tf/test.xml");
task.execute(); 
于 2013-12-10T11:52:48.663 回答
0

在任何应用程序中,您都应该避免在主线程上调用 IO,因为它通常用于处理用户事件和 UI。在 android 中这样做会导致NetworkOnMainThreadException

尝试将您的网络调用移动到后台线程,它应该可以工作。

前任

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");
}
} 

请查看本教程以更好地理解 android threading。教程

于 2013-12-10T11:55:14.113 回答