似乎Thread.start()没有调用run方法

时间:2015-11-18 06:46:03

标签: android multithreading

private void showLocation(final Location location){
    new Thread(new Runnable(){
        HttpURLConnection connection;
        @Override
        public void run() {
            try{
                Toast.makeText(MainActivity.this, "here", Toast.LENGTH_SHORT).show();
                StringBuilder url=new StringBuilder();
                url.append("http://maps.googleapis.com/maps/api/geocode/json?latlng=");
                url.append(location.getLatitude());
                url.append(",");
                url.append(location.getLongitude());
                url.append("&sensor=false");
                URL Url=new URL(url.toString());
                connection=(HttpURLConnection)Url.openConnection();
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(8000);
                connection.setReadTimeout(8000);
                InputStream in=connection.getInputStream();
                BufferedReader reader=new BufferedReader(new InputStreamReader(in));
                StringBuilder response=new StringBuilder();
                String line;
                while((line=reader.readLine())!=null){
                    response.append(line);
                }
                JSONObject jsonObject=new JSONObject(response.toString());
                JSONArray resultArray=jsonObject.getJSONArray("results");
                if(resultArray.length()>0){
                    JSONObject subObject=resultArray.getJSONObject(0);
                    String address=subObject.getString("formatted_address");
                    Toast.makeText(MainActivity.this, address, Toast.LENGTH_SHORT).show();
                    Message message=new Message();
                    message.what=SHOW_LOCATION;
                    message.obj=address;
                    handler.sendMessage(message);
                }
            }catch(Exception e){
                e.printStackTrace();
            }finally {
                if(connection!=null){
                    connection.disconnect();
                    connection=null;
                }
            }
        }
    }).start();

}

我使用此方法通过使用Goole map Api显示有关位置的详细信息。 但似乎由于App的活动而从未调用run()。 我已经搜索了这个问题,但没有解决它。 我是andoird的新手,我无法找到这个bug。需要帮助。

2 个答案:

答案 0 :(得分:0)

检查您的日志。我的猜测是因为你试图从一个线程创建Toast而出现错误。对于Toast命令以及影响ui的所有其他调用,您应该将其包装在runOnUiThread()中。

答案 1 :(得分:0)

您无法从非UI线程更新UI。您需要删除Toast调用。 试试这个..

private void showLocation(final Location location){
    new Thread(new Runnable(){
        HttpURLConnection connection;
        @Override
        public void run() {
            try{
                StringBuilder url=new StringBuilder();
                url.append("http://maps.googleapis.com/maps/api/geocode/json?latlng=");
                url.append(location.getLatitude());
                url.append(",");
                url.append(location.getLongitude());
                url.append("&sensor=false");
                URL Url=new URL(url.toString());
                connection=(HttpURLConnection)Url.openConnection();
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(8000);
                connection.setReadTimeout(8000);
                InputStream in=connection.getInputStream();
                BufferedReader reader=new BufferedReader(new InputStreamReader(in));
                StringBuilder response=new StringBuilder();
                String line;
                while((line=reader.readLine())!=null){
                    response.append(line);
                }
                JSONObject jsonObject=new JSONObject(response.toString());
                JSONArray resultArray=jsonObject.getJSONArray("results");
                if(resultArray.length()>0){
                    JSONObject subObject=resultArray.getJSONObject(0);
                    String address=subObject.getString("formatted_address");
                    Log.e("address",address);
//                    Toast.makeText(MainActivity.this, address, Toast.LENGTH_SHORT).show();
                    Message message=new Message();
                    message.what=SHOW_LOCATION;
                    message.obj=address;
                    handler.sendMessage(message);
                }
            }catch(Exception e){
                e.printStackTrace();
            }finally {
                if(connection!=null){
                    connection.disconnect();
                    connection=null;
                }
            }
        }
    }).start();

}

并查看日志..注意标记"地址"。