【问题标题】:Android - retrieve data from diffrent php/url and update the textview based on return value of urlAndroid - 从不同的 php/url 检索数据并根据 url 的返回值更新 textview
【发布时间】:2017-01-05 06:18:29
【问题描述】:

我正在尝试根据 URL 的返回值更新 textview。第一个 URL,返回值更新 textview。但是第二个 url,textview 没有更新数据,它只是空白。

公共类 MainActivity 扩展 AppCompatActivity {

// CONNECTION_TIMEOUT and READ_TIMEOUT are in milliseconds
public static final int CONNECTION_TIMEOUT = 10000;
public static final int READ_TIMEOUT = 15000;
TextView textHost, textOS;

StringBuilder result, resultOS;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textHost = (TextView) findViewById(R.id.textHost);
    textOS = (TextView) findViewById(R.id.textOS);

    //Make call to AsyncRetrieve
    new AsyncRetrieve().execute();
}

private class AsyncRetrieve extends AsyncTask<String, String, String>
{
    ProgressDialog pdLoading = new ProgressDialog(MainActivity.this);
    HttpURLConnection conn, connOS;
    URL url, urlOS = null;

    //this method will interact with UI, here display loading message
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pdLoading.setMessage("\tLoading...");
        pdLoading.setCancelable(false);
        pdLoading.show();

    }

    // This method does not interact with UI, You need to pass result to onPostExecute to display
    @Override
    protected String doInBackground(String... params)
    {
        try
        {
            // Enter URL address where your php file resides
            url = new URL("http://192.168.1.13/fyp/Cpu.php");
            urlOS = new URL ("http://192.168.1.13/fyp/Name.php");

        } catch (MalformedURLException e)
          {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return e.toString();
          }

        try
        {
            // Setup HttpURLConnection class to send and receive data from php
            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(READ_TIMEOUT);
            conn.setConnectTimeout(CONNECTION_TIMEOUT);
            conn.setRequestMethod("GET");

            conn.setDoOutput(true);

            connOS = (HttpURLConnection) urlOS.openConnection();
            connOS.setReadTimeout(READ_TIMEOUT);
            connOS.setConnectTimeout(CONNECTION_TIMEOUT);
            connOS.setRequestMethod("GET");

            // setDoOutput to true as we recieve data from json file
            connOS.setDoOutput(true);

        } catch (IOException e1)
          {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            return e1.toString();
          }

        try
        {
            int response_code = conn.getResponseCode();
            int responseOS = connOS.getResponseCode();

            // Check if successful connection made
            if (response_code == HttpURLConnection.HTTP_OK)
            {
                // Read data sent from server
                InputStream input = conn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                result = new StringBuilder();
                String line;

                while ((line = reader.readLine()) != null)
                {
                    result.append(line);
                }

                // Pass data to onPostExecute method
                return (result.toString());

            }

            if (responseOS == HttpURLConnection.HTTP_OK)
            {
                // Read data sent from server
                InputStream input = connOS.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                resultOS = new StringBuilder();
                String line;

                while ((line = reader.readLine()) != null)
                {
                    resultOS.append(line);
                }

                // Pass data to onPostExecute method
                return (resultOS.toString());
            }

            else
            {
                return ("unsuccessful");
            }

        } catch (IOException e)
          {
            e.printStackTrace();
            return e.toString();
          } finally
            {
                conn.disconnect();
                connOS.disconnect();
            }


    }

    public void output()
    {

            textHost.setText(result); //the textview is updated based on data fetch on url
            textOS.setText(resultOS); //the data does not show

    }

        // this method will interact with UI, display result sent from doInBackground method
    @Override
    protected void onPostExecute(String result)
    {
        super.onPostExecute(result);

        pdLoading.dismiss();

            output();
            //textHost.setText(result.toString());
            //textOS.setText(resultOS.toString());


    }

}

}

【问题讨论】:

  • 欢迎来到 StackOverflow。您将需要调试此程序并提供更多信息,因为可能有很多原因导致这种情况没有发生(服务器关闭、未连接到 Internet 等)。小程序调试请看本指南:ericlippert.com/2014/03/05/how-to-debug-small-programs

标签: php android textview httpurlconnection


【解决方案1】:

//此方法将与UI交互,显示doInBackground方法发送的结果

@Override
protected void onPostExecute(String result)
{
    super.onPostExecute(result);

    pdLoading.dismiss();

        output();
          //Uncomment the below line 
        textHost.setText(result.toString());
        textOS.setText(resultOS.toString());


}

【讨论】:

  • 谢谢。我已经取消注释代码,但 resultOS 仍然没有出现在 textOS 中。它只是空白。我认为它不会执行 doInBackground 中的代码。
猜你喜欢
  • 2020-06-22
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2018-03-14
  • 1970-01-01
  • 2020-04-12
相关资源
最近更新 更多