gpt4 book ai didi

java - 在主线程上显示 XML

转载 作者:行者123 更新时间:2023-12-01 12:59:09 24 4
gpt4 key购买 nike

我有一个小问题,我不明白。

这只是一个简单的请求:如何显示我刚刚在线程中获得的 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();
}

}

它出现在我的Log中,但不在TextView中:/我有一个白色的空Activity

最佳答案

问题是您在 UI-tread 上调用 postData(),这意味着该方法还会在 UI 上返回 theCampagneXml -thread,而您的网络操作在工作线程上进行。以下代码经过一些更改和添加后修复了该问题:

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

网络操作完成并初始化 theCampagneXml 后,对在 UI 线程上运行的 TextView Campagne 使用 post()

更多信息可以在Processes and Threads中找到.

关于java - 在主线程上显示 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23658726/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com