Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
513 views
in Technique[技术] by (71.8m points)

java - Can't create handler inside thread that has not called Looper.prepare() Android

I am using AsyncTask to call yahooweather API. Following is the code:


public class myactivity extends Activity {
    final String yahooapisBase = "http://query.yahooapis.com/v1/public/yql?q=select*from%20geo.places%20where%20text=";
    final String yahooapisFormat = "&format=xml";
    String yahooAPIsQuery;

EditText input_city;
EditText input_zip;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myactivity);
    Button btn_findout = (Button) findViewById(R.id.btn_findout);
    input_city = (EditText) findViewById(R.id.input_cityOrcountry);
    input_zip = (EditText) findViewById(R.id.input_zip);

    // when Zip textbox has focus
    input_zip.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                String string = "";
                input_city.setText(string);
            }
        }
    });

    // when city/country textbox has focus
    input_city.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                String string = "";
                input_zip.setText(string);
            }
        }
    });

    // when findout button is clicked
    btn_findout.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            String uriPlace = Uri.encode(input_city.getText().toString());
            yahooAPIsQuery = yahooapisBase + "%22" + uriPlace + "%22"
                    + yahooapisFormat;
            Toast.makeText(getBaseContext(),
                    "Before entering into sub thread", Toast.LENGTH_LONG)
                    .show();
            new WeatherAPITask().execute(yahooAPIsQuery);
            Toast.makeText(getBaseContext(), "After sub thread",
                    Toast.LENGTH_LONG).show();
            Log.i("my label", "back in main thread...");
            // String woeidString = QueryYahooWeather(yahooAPIsQuery);
            // input_city.setText(woeidString);
        }
    });
}

private String QueryYahooWeather(String queryString) {

    String qResult = "";

    HttpClient httpClient = new DefaultHttpClient();

    HttpGet httpGet = new HttpGet(queryString);

    try {
        Log.i("WeatherApp", "digging into try block...");
        Log.i("queryString", queryString);
        HttpEntity httpEntity = httpClient.execute(httpGet).getEntity();

        if (httpEntity != null) {
            InputStream inputStream = httpEntity.getContent();
            Reader in = new InputStreamReader(inputStream);
            BufferedReader bufferedreader = new BufferedReader(in);
            StringBuilder stringBuilder = new StringBuilder();

            String stringReadLine = null;

            while ((stringReadLine = bufferedreader.readLine()) != null) {
                stringBuilder.append(stringReadLine + "
");
            }

            qResult = stringBuilder.toString();
        }

    } catch (ClientProtocolException e) {
        e.printStackTrace();
        Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
                .show();
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
                .show();
    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
                .show();
    }
    Toast.makeText(getBaseContext(), "Returning from function",
            Toast.LENGTH_LONG).show();
    return qResult;

}

private class WeatherAPITask extends AsyncTask {

    protected void onPostExecute(String result) {
        Log.i("my label", "entering in onPostExecute");
        Log.i("result", result);
        input_city.setText("result");
    }

    @Override
    protected Object doInBackground(Object... params) {
        // TODO Auto-generated method stub
        try {
            Log.i("my label", "entering in doInBackground");
            Log.i("params[0]", params[0].toString());
            return QueryYahooWeather(params[0].toString());
        } catch (Exception e) {
            Log.i("my label", e.toString());
            return null;
        }
    }
}

}

After debugging the code I found that the yahooAPI call is successfull and I can see the XML response in QueryYahooWeather function. But as soon as execution of this function completes an exception has been thrown: Can't create handler inside thread that has not called Looper.prepare()

Please help me out.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Remove all Toast's from QueryYahooWeather method because this method is called from doInBackground(Object... params) of AsyncTask and you can not Access Ui elements like Toast(also an Ui element)from background Thread.

NOTE : if you want to known what's going on in background then use Log instead of Toast's

EDIT:

Change doInBackground as :

@Override
protected String doInBackground(Object... params) {
    // TODO Auto-generated method stub
    String strresult="";
    try {
        Log.i("my label", "entering in doInBackground");
        Log.i("params[0]", params[0].toString());
         strresult= QueryYahooWeather(params[0].toString());
         Log.i("strresult result ::: ", strresult);

    } catch (Exception e) {
        Log.i("my label", e.toString());
        return null;
    }
 return strresult;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...