Java 在主线程上显示XML
我对一件我不明白的小事有点意见 这只是一个简单的请求:如何显示我刚进入线程的Java 在主线程上显示XML,java,android,multithreading,ui-thread,Java,Android,Multithreading,Ui Thread,我对一件我不明白的小事有点意见 这只是一个简单的请求:如何显示我刚进入线程的xml 有我的方法postData来获取xml,我让它显示在log.v中,正如您在下面的代码中看到的那样,但我不能将它显示在线程外的TextView中 public class RecupXml_Activity extends Activity { TextView campagne; String user = "toto"; String password = "tata"; String theCampagne
xml
有我的方法postData
来获取xml
,我让它显示在log.v
中,正如您在下面的代码中看到的那样,但我不能将它显示在线程外的TextView
中
public class RecupXml_Activity extends Activity {
TextView campagne;
String user = "toto";
String password = "tata";
String theCampagneXml;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
campagne = (TextView) findViewById(R.id.campagneTest);
postData(user, password);
}
public void postData(final String login, final String password) {
Thread background = new Thread(new Runnable() {
URL url;
String buffer;
String theCampagneXml = null;
@Override
public void run() {
try {
URLConnection urlConnection;
String body = "login=" + URLEncoder.encode(login, "UTF-8") + "&password=" + URLEncoder.encode(password, "UTF-8");
url = new URL("http://3pi.tf/apps/sms/");
urlConnection = url.openConnection();
((HttpURLConnection) urlConnection).setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConnection.setRequestProperty("Content-Length", "" + body.length());
OutputStreamWriter writer = null;
BufferedReader reader = null;
writer = new OutputStreamWriter(urlConnection.getOutputStream());
writer.write(body);
writer.flush();
reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
while ((buffer = reader.readLine()) != null) {
theCampagneXml = buffer;
}
Log.v("test", "xml = " + theCampagneXml);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
campagne.post(new Runnable() {
@Override
public void run() {
campagne.setText("salut voici ta campagne : " + theCampagneXml);
}
});
}
});
background.start();
}
}
它出现在我的
日志中,但不出现在文本视图中。:/I有一个白色的空活动问题是您在UI线程上调用postData()
,这意味着当您的网络操作在工作线程上进行时,该方法也会在UI线程上返回CampagnExml
。以下代码进行了一些更改和添加,修复了该问题:
public class MainActivity extends Activity {
TextView campagne;
String user = "toto";
String password = "tata";
String theCampagneXml; // new
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
campagne = (TextView) findViewById(R.id.text);
postData(user, password); // new
}
public void postData(final String login, final String password) { // note: the return type has been changed
Thread background = new Thread(new Runnable() {
URL url;
String buffer;
String theCampagneXml = null; // new
@Override
public void run() {
try {
// no changes here but declaring `theCampagneXml` as class member
}
campagne.post(new Runnable() {
@Override
public void run() {
campagne.setText("hello, here is your XML : "+ theCampagneXml);
}
});
}
});
background.start();
}
}
一旦网络操作完成并且初始化了CampagnExML
,对UI线程上运行的TextView campagne
使用post()
其他信息可在中找到