Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 安卓4.0中的类没有响应-适用于2.3及以下版本_Java_Android - Fatal编程技术网

Java 安卓4.0中的类没有响应-适用于2.3及以下版本

Java 安卓4.0中的类没有响应-适用于2.3及以下版本,java,android,Java,Android,我有一个类GetWeather.java,它从API获取天气数据。它是从我的应用程序的主活动通过一个单独的线程定期调用的。线程点击GetWeather类并将返回的数据发布到TextView 在任何一种情况下,GetWeather类中返回的数据的System.out.println都表明数据确实在返回 下面是GetWeather.java: import java.io.BufferedReader; import java.io.InputStream; import java.io.Input

我有一个类GetWeather.java,它从API获取天气数据。它是从我的应用程序的主活动通过一个单独的线程定期调用的。线程点击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());
        }
    }
}
正如我前面提到的,这在姜饼和
FroYo
中可以正常工作,但是
ICS
JellyBean
OS无法看到GetWeather设置的变量。我想,我在某个地方读到,这与需要一个异步任务有关,但我不知道它的头绪


提前感谢

尝试使用类似这样的异步任务

在你的主要活动中

public class GetWeather extends AsyncTask<Void, Integer, Void> {



    public GetWeather(Activity activity) {
        this.activity = activity;


        context = activity;
        dialog = new ProgressDialog(context);


    }

    /** progress dialog to show user that the backup is processing. */
    private ProgressDialog dialog;
    /** application context. */
    private Activity activity;
    private Context context;



    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();






    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub


        GetWeather weather = new GetWeather(strPosition);
        weather.returnWeather();


        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);



                     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);
        }




                     }



  }

尝试像这样使用异步任务

在你的主要活动中

public class GetWeather extends AsyncTask<Void, Integer, Void> {



    public GetWeather(Activity activity) {
        this.activity = activity;


        context = activity;
        dialog = new ProgressDialog(context);


    }

    /** progress dialog to show user that the backup is processing. */
    private ProgressDialog dialog;
    /** application context. */
    private Activity activity;
    private Context context;



    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();






    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub


        GetWeather weather = new GetWeather(strPosition);
        weather.returnWeather();


        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);



                     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);
        }




                     }



  }

您不能从后台线程中触摸ui线程中的任何内容,要使用处理程序进行此操作,请初始化后台线程并向其传递处理程序对象。当数据到达时,使用处理程序向ui发送消息。在ui中,当来自后台线程的消息出现时,只需更新视图。

您不能从后台线程接触ui线程中的任何内容,为此,请使用处理程序初始化后台线程,并向其传递处理程序对象。当数据到达时,使用处理程序向ui发送消息。在ui中,当来自后台线程的消息出现时,只需更新视图。

如果您可以放置任何日志,请!!看起来您可能正在尝试从UI线程以外的其他内容更新UI?@ChrisStratton yep。事实上,我从来没有想到过这个问题。如果你能把任何日志放进去,请!!看起来您可能正在尝试从UI线程以外的其他内容更新UI?@ChrisStratton yep。事实上,我从来没有想到过这个问题。谢谢你。今晚我会研究一下,看看是否可以将其合并到现有的项目中。谢谢。今晚我将对此进行调查,看看是否可以将其合并到现有项目中。
new GetWeather(Mainactivity.this).execute();