在天气API中找到一个城市

时间:2018-01-01 17:26:12

标签: android

我是新手Android ..我要建立天气Api但我在某个地方取得了成功,有些地方失败了。我想帮助..在我的代码中我成功地搜索了城市但是最糟糕的是我有在我搜索城市时失败的那些不存在的生活意味着我有搜索伦敦'它给了我答案,当我搜索Londo'应用程序停止并强制关闭。而不是强制关闭我希望“找不到城市”#39;正如我除外但我的应用程序破碎...请帮助我..抱歉我的语法...

public class MainActivity extends AppCompatActivity {

    TextView t1,t2,t3,t4;
    public String theString,search;
    EditText e1;
    JSONObject jsonWeather = null;
    Button b1;


    public static boolean isConnectingToInternet(Context context) {

        ConnectivityManager cm =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();

        return activeNetwork != null &&
                activeNetwork.isConnectedOrConnecting();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        t1=(TextView)findViewById(R.id.T1);
        t2=(TextView)findViewById(R.id.T2);
        e1=(EditText)findViewById(R.id.E1);
        b1=(Button)findViewById(R.id.B1);
        t3=(TextView)findViewById(R.id.T3);
        t4=(TextView)findViewById(R.id.T4);
       if(!isConnectingToInternet(this))
             t2.setText("Connection Not Established ");
         else{
           b1.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                  new MyAsyncTask().execute();
               }
           });
         }
    }
    public class MyAsyncTask extends AsyncTask<String, Integer, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            search=e1.getText().toString();
        }

        @Override
        protected String doInBackground(String... strings) {

                try {
                    InputStream input = new URL(  "http://api.openweathermap.org/data/2.5/forecast?q="+ search +",in&appid=90367b057052fc27a3c059a4b37474f9").openStream();

                    Log.e("ADebugTag11", "Value: " + input);
                    theString = IOUtils.toString(input, "UTF-8");

                    Log.e("ADebugTag", "Value: " + theString);
                    return  theString;

                } catch (IOException e ) {
                    throw new RuntimeException(e);
                }

        }

        @Override
        protected void onPostExecute(String aLong) {
            super.onPostExecute(aLong);

            try{
                    jsonWeather =new JSONObject(aLong);
                    if(jsonWeather.getInt("cod")==400){

                        t1.setText(""+jsonWeather.getString("message"));
                        t2.setText("");
                        t3.setText("");
                    }
                    else{

                        t1.setText(jsonWeather.getJSONObject("city").getString("country"));
                        t2.setText(jsonWeather.getJSONObject("city").getString("name"));
                    }


            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}

0 个答案:

没有答案