【问题标题】:Informationparsing from URL-Request not working / Didn't found TextView来自 URL 请求的信息解析不起作用/未找到 TextView
【发布时间】:2016-01-06 00:26:02
【问题描述】:

我需要你的帮助。我想发送一个 URL 请求,获取响应并创建一个 JSON 对象。我的第一次尝试完全错误。现在我找到了一个教程,做了一个新的尝试。

我的活动看起来像:

public class Patienten extends Activity {

//Beacon Elemente
private String UUID;
private String Major;
private String Minor;

private TextView output;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_patienten);
    output = (TextView) findViewById(R.id.output);
    UpdateBeaconInformation();

    Button cmdHit = (Button) findViewById(R.id.cmd_hit);
    cmdHit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new JSONTask().execute("//http://kusber-web.de/JsonTest.txt");
        }
    });


    setTitle(Surname + ", " + FirstName);
   // output.setText(output.getText().toString() + "Gefundener Patient:\n" + "Name: " + Surname + ", " + FirstName + "\nGeb.-Dat: " + Birthdate);
}

然后我创建了一个新的 Java 类并用它构建了一个 asyncTask。但我无法访问 onPostExecute 中的 textview 输出来更新它。

public class JSONTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... urls) {
    HttpURLConnection connection = null;
    BufferedReader reader = null;
    try {

        //http://kusber-web.de/JsonTest.txt
        //http://nilsbenning.selfhost.me/PatientFinder.php?beacon_comID=5181f8a3-7354-46ac-b22d-952ec395ab06&beacon_major=12&beacon_minor=249
        URL url = new URL(urls[0]);
        connection = (HttpURLConnection) url.openConnection();
        connection.connect();

        InputStream stream = connection.getInputStream();

        reader = new BufferedReader(new InputStreamReader(stream));

        StringBuffer buffer = new StringBuffer();
        String line;
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }

        return buffer.toString();

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    return null;
}

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

}

} 我的错误是什么?为什么我无法访问它?我在这里看到它是一个解决方案,但没有让它发挥作用:

https://stackoverflow.com/a/12252717/5743912

希望你现在可以帮助我! :)

【问题讨论】:

  • “我的错误是什么?” -- 除其他外,您正在主应用程序线程上执行网络 I/O,并且您正在捕获异常而不记录它们。
  • 好吧,你是对的@CommonsWare。对不起,我对 android 开发完全陌生。我在最后几个小时学到了很多东西并创造了一种新的方式。你现在能帮我找出错误吗??
  • 您的网址中有 // BEFORE http。尝试在异常处理程序和 finally 子句中设置断点。这可能会帮助您找到问题所在。
  • @Jim Rhodes 是的 // 供我参考。 URL 被称为 .execute... 中的参数。我无法在那里设置断点,因为当错误中有 output.setText 时无法编译。我无法从 AsyncTask 类中的 Patient Activity 访问 TextView。但我不明白如何使用这个。如何获取从 AsyncTask 中的 URL 连接到我的 Patienten Activity 的结果字符串?

标签: android json url-parsing


【解决方案1】:

您可能想要解决这个问题(删除前导斜杠):

new JSONTask().execute("//http://kusber-web.de/JsonTest.txt");

在您的 JSONTask 中,您可以使用 Patienten.this 引用 Patienten 的成员。所以在onPostExecute 你应该改变这个:

output.setText(result);

到:

Patienten.this.output.setText(result);

【讨论】:

  • 你说得对//我多么愚蠢!现在我得到 com.projektarbeit.Patienten 不是封闭类。如何解决?我应该在 AsyncTask 中启动它吗?这样做是因为 Patienten 正在调用 AsyncTask 然后在那里启动它??
  • @AndroidNewbee 您应该将您的 JSONTask 定义为 Patienten 的内部类。
  • 我真的是个新手 :D 以前从未听过内部阶层的消息。在 Google 上阅读它,复制并粘贴它,它就可以运行了!太简单!非常感谢!!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-21
  • 1970-01-01
  • 2019-02-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多