1

我一直在尝试使用 JSON/Php Web 服务层通过我的 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”,就好像它从未更改过一样。这与单独的进程有关吗?我能做些什么呢?

4

1 回答 1

1

您已经解析了数据,但从未再次设置为 textview。

您可以在 onPostExecute 中执行此操作,此方法将在 UI 线程上调用,因此它是安全的。(切勿尝试在 doInBackground 中设置任何会导致异常的视图属性)

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();
}
于 2013-04-12T04:41:30.810 回答