duanmi4379 2017-01-05 06:18
浏览 48

Android - 从不同的php / url检索数据,并根据url的返回值更新textview

I'm trying to update textview based on return value from the URL. First URL,the return value update the textview. but the second url, the textview does not update with data, it only blank.

public class MainActivity extends 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());


    }

}

}

  • 写回答

1条回答 默认 最新

  • dseax40600 2017-01-05 08:40
    关注

    // this method will interact with UI, display result sent from doInBackground method

    @Override
    protected void onPostExecute(String result)
    {
        super.onPostExecute(result);
    
        pdLoading.dismiss();
    
            output();
              //Uncomment the below line 
            textHost.setText(result.toString());
            textOS.setText(resultOS.toString());
    
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型