gpt4 book ai didi

android - 传回在单独线程中创建的变量?安卓

转载 作者:行者123 更新时间:2023-11-30 03:39:49 28 4
gpt4 key购买 nike

我一直在尝试使用 JSON/Php 网络服务层通过我的 android 应用程序访问 MySQL 数据库并且它有效(从日志中我可以看到变量保存了正确的信息)但是当我尝试并通过返回主线程的值没有被存储?

public class ViewErrands extends Activity {
public String result = "";
InputStream is;
String json = "lol"; //set as lol to begin with, to be overwritten with string from DB
TextView tv;
public int iss;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
tv = (TextView) findViewById(R.id.tvtv);
sendPostRequest();
tv.setText(json + "LOL" + iss); // the "lol" + "LOL" + 0 (should be 6) appear
}

public void sendPostRequest() {
class runCode extends AsyncTask<String, String, String> { // seperate task

@Override
protected String doInBackground(String... params) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("idnum", "1"));

try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.182/FYP/xshowall.php"); // PHP script to show all where id > POST
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "error in http conn " + e.toString());
}

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "error converting " + e.toString());
}

try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
json = "id: " + json_data.getInt("ID") + ", title: " + json_data.getString("TITLE")
+ ", content: " + json_data.getString("CONTENT");

);

}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return null;
}
}
runCode sendPostReqAsyncTask = new runCode();
sendPostReqAsyncTask.execute();
}

首先,当我尝试复制一个值时,json 变量保持为“lol”,就好像它从未更改过一样。这与单独的过程有关吗?我该怎么办?

最佳答案

你已经解析了数据,但再也没有设置为textview。

您可以在 onPostExecute 中执行此操作,此方法将在 UI 线程上调用,因此它是安全的。 (永远不要尝试在 doInBackground 中设置任何 View 的属性,这会导致您出现异常)

public void sendPostRequest() {
class runCode extends AsyncTask<String, String, String> { // seperate task

@Override
protected String doInBackground(String... params) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("idnum", "1"));

try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.182/FYP/xshowall.php"); // PHP script to show all where id > POST
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "error in http conn " + e.toString());
}

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "error converting " + e.toString());
}

try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
json = "id: " + json_data.getInt("ID") + ", title: " + json_data.getString("TITLE")
+ ", content: " + json_data.getString("CONTENT");

);

}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return null;
}
@Override
protected void onPostExecute(String result) {
tv.setText(json);
}
}
runCode sendPostReqAsyncTask = new runCode();
sendPostReqAsyncTask.execute();
}

关于android - 传回在单独线程中创建的变量?安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15962876/

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