【问题标题】:I get no results when getting data from database, why? java(android studio从数据库获取数据时没有结果,为什么? java(安卓工作室
【发布时间】:2018-11-20 13:55:46
【问题描述】:

在调试中我可以看到它把我需要的所有东西都放入了 sb StringBuilders,但是在第二个 sb 之后数据就消失了,之前写入到安全字符串中的内容也被删除了,不知道为什么。

这是整个代码,自从我发布以来,它做了一些更改。 还是一样,这不是超出范围,因为我确实收到了getting_kat的结果,只是缺少getting_ter的结果。

@Override
public String doInBackground(String... voids) {

    String result="";
    result=getting_kat();
    result+=getting_ter();
    //Making sure it gets put into the safety string
    safety_string=result;
    return result;
}

public String getting_kat(){
    String result="";
    String resultkat = null;
    String connstr = "My connection string";
    try {
        URL url = new URL(connstr);
        HttpURLConnection http = (HttpURLConnection) url.openConnection();
        http.setRequestMethod("POST");
        http.setDoInput(true);
        http.setDoOutput(true);


        InputStream ips = http.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(ips, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();
        String line = "";

        //Line by Line reading the data from php

        while ((line = br.readLine()) != null) {
            sb.append(line+"\n");
        }

        //String it together into one line
        resultkat = sb.toString();

        //Adding a separator for later split
        result=resultkat+"#KATEGORIA/TERMEK#";

        br.close();
        ips.close();
        http.disconnect();

    } catch (MalformedURLException e) {
        result = e.getMessage();
    } catch (IOException e) {
        result = e.getMessage();
    }
    return result;
}

public String getting_ter(){
    String result="";
    String resultter=null;
    String connstr2 = "My connection string 2";



    try {
        URL url = new URL(connstr2);
        HttpURLConnection http = (HttpURLConnection) url.openConnection();
        http.setRequestMethod("POST");
        http.setDoInput(true);
        http.setDoOutput(true);

        InputStream ips = http.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(ips, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();
        String line = "";

        //Line by Line reading the data from php

        while ((line = br.readLine()) != null) {
            sb.append(line+"\n");
        }

        //String it together into one line
        resultter = sb.toString();

        result=resultter;

        br.close();
        ips.close();
        http.disconnect();

    } catch (MalformedURLException e) {
        result = e.getMessage();
    } catch (IOException e) {
        result = e.getMessage();
    }
    return result;
}

【问题讨论】:

  • 我建议您学习 DRY 的含义。代码重复不好。您应该将这里所做的工作封装在一个更简单的方法中,您可以在旁边进行测试和使用。我打赌这个问题与范围有关,但如果不查看整个方法就很难判断。

标签: java database android-studio


【解决方案1】:

我看不到代码中的错误。我为您清理了一下,但它应该在我更改之前和之后都可以使用。你没有描述它实际上出了什么问题。在调试过程中,您的连接可能会超时。

public String doInBackground(String... voids) {
    String result = getting_kat() + "#KATEGORIA/TERMEK#" + getting_ter();
    //Making sure it gets put into the safety string
    safety_string = result;
    return result;
}

public String getting_kat() {
    String result = "";
    String connstr = "My connection string";

    try {
        HttpURLConnection http = connect(connstr);
        result = readFromPhp(http);
        http.disconnect();
    } catch (MalformedURLException e) {
        result = e.getMessage();
    } catch (IOException e) {
        result = e.getMessage();
    }
    return result;
}

public String getting_ter() {
    String result = "";
    String connstr = "My connection string 2";

    try {
        HttpURLConnection http = connect(connstr);
        result = readFromPhp(http);
        http.disconnect();
    } catch (MalformedURLException e) {
        result = e.getMessage();
    } catch (IOException e) {
        result = e.getMessage();
    }
    return result;
}

private HttpURLConnection connect(String connstr) throws IOException {
    URL url = new URL(connstr);
    HttpURLConnection http = (HttpURLConnection) url.openConnection();
    http.setRequestMethod("POST");
    http.setDoInput(true);
    http.setDoOutput(true);
    return http;
}

private String readFromPhp(HttpURLConnection http) throws IOException {
    // try-with-resources - so you don't have to call close()
    try (
            InputStream ips = http.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(ips, "UTF-8"), 8)
    ) {
        StringBuilder sb = new StringBuilder();

        String line;
        //Line by Line reading the data from php
        while ((line = br.readLine()) != null) {
            sb.append(line).append("\n");
        }

        //String it together into one line
        return sb.toString();
    }
}

【讨论】:

  • 它不是连接,在调试时,当我填充字符串生成器时,我可以看到它获取了数据,但在那之后,数据就被删除了。顺便说一句,我修复了它,我知道它是如何工作的,但我只是使用我的安全字符串在 while() 中接收数据并且它有效,但无论如何感谢大家的帮助
  • 我尝试使用你的代码,结果两者都没有,然后我引入了两个新的公共字符串 safe_string_kat 和 ..._ter,kat 工作,ter 保持为空,很奇怪,当我调试时再次可以看到所有到达的数据都被放入字符串构建器中,但我仍然没有在 and 处得到任何数据,但就像我说只有在调用 getting_ter 时... fml
  • 什么叫“result is nothing”,这段代码在你的程序中是怎么调用的,这部分属于什么类?
  • 这是一个 AsyncTask 吗?
猜你喜欢
  • 1970-01-01
  • 2019-05-13
  • 2018-03-05
  • 2014-04-30
  • 1970-01-01
  • 2017-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多