gpt4 book ai didi

java - Android 4.0 中的类没有响应 - 适用于 2.3 及更低版本

转载 作者:行者123 更新时间:2023-12-01 23:47:17 26 4
gpt4 key购买 nike

我有一个类 GetWeather.java,它从 API 获取天气数据。它是通过一个单独的线程定期从我的应用程序的主要 Activity 中调用的。该线程调用 GetWeather 类并将返回的数据发布到 TextView。

无论哪种情况,GetWeather 类中返回的数据的 System.out.println 都显示数据确实正在返回。

下面是 GetWeather.java:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.net.Uri;
import android.util.Log;

public class GetWeather {

InputStream is = null;

JSONArray jArray = null;
JSONObject json_data = new JSONObject();
String result = "";

String strTemp = "";
String strWindSpeed = "";
String strWindDir = "";
String strVisibility = "";

String strPosition = "";

public static final Uri KEY_121 = Uri.parse("http://api.worldweatheronline.com/free/v1/weather.ashx");
String strWeatherApiKey = "REMOVED";

public GetWeather(String Location) {
strPosition = Location;
}

public void returnWeather() {
try {
HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost(KEY_121 + "?key="
+ strWeatherApiKey + "&q=" + strPosition + "&format=json");

HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();

} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}

// convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 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 result " + e.toString());
}
// parse json data
try {
JSONObject object = new JSONObject(result);
JSONObject weather = object.getJSONObject("data");
JSONArray current_conditions = weather
.getJSONArray("current_condition");
for (int i = 0; i < current_conditions.length(); i++) {
JSONObject object1 = (JSONObject) current_conditions.get(i);
strTemp = object1.getString("temp_C");
strWindSpeed = object1.getString("windspeedMiles");
strWindDir = object1.getString("winddir16Point");
strVisibility = object1.getString("visibility");

// Testing output
System.out.println(strTemp);
System.out.println(strWindSpeed);
System.out.println(strWindDir);
System.out.println(strVisibility);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
}
}

以下是主要 Activity 的相关代码:

Runnable updateConsoleRunnable = new Runnable() {
public void run() {
tvConsole.setMovementMethod(new ScrollingMovementMethod());
tvConsole.setSelected(true);

handler.postDelayed(this, TIME_DELAY);

// Only display weather data while service is enabled
if (isServiceRunning()) {
GetWeather weather = new GetWeather(strPosition);
weather.returnWeather();

// Weather package
tvConsole
.append("Weather Update\n-------------------\n\nCurrent Temp (C): "
+ weather.strTemp
+ "C\n"
+ "Wind is out of the "
+ weather.strWindDir
+ " at "
+ weather.strWindSpeed
+ " MPH\n"
+ "Visibility is "
+ weather.strVisibility
+ " miles\n\n");

// Auto-scroll textview
// Does not function on Android 4.0+
final Layout layout = tvConsole.getLayout();
if (layout != null) {
int scrollDelta = layout.getLineBottom(tvConsole
.getLineCount() - 1)
- tvConsole.getScrollY()
- tvConsole.getHeight();
if (scrollDelta > 0)
tvConsole.scrollBy(0, scrollDelta);
}

}

}
};

正如我之前提到的,这在 Gingerbread 和 FroYo 中按预期工作,但是 ICSJellyBean 操作系统无法看到由获取天气。我想,我在某处读到这与需要 AsyncTask 有关,但我无法弄清楚它的头部或尾部。

提前致谢

最佳答案

您无法从后台线程触摸 ui 线程中的任何内容,为此,请使用处理程序,初始化您的后台线程,并向其传递一个处理程序对象。当数据到达时,使用处理程序向用户界面发送消息。在ui中,当后台线程的消息到来时,只需更新 View 即可。

关于java - Android 4.0 中的类没有响应 - 适用于 2.3 及更低版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16835723/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com