2017-07-01 115 views
-1

我對Java非常陌生,一直在製作天氣應用程序。 我只是使用主要活動來製作天氣應用程序。我想通過城市功能添加天氣。我傾向於通過構建可在下面的代碼中看到的url來實現這一點。 「url」+ cityname +「apikey」。現在我想從用戶處取得城市名稱,然後我希望應用程序刷新並在此之後發送新的請求。如何將輸入添加到變量?

package com.example.sabi.thisapp; 

import android.content.Context; 
import android.content.SharedPreferences; 
import android.support.design.widget.TextInputEditText; 
import android.support.design.widget.TextInputLayout; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

import com.android.volley.Request; 
import com.android.volley.RequestQueue; 
import com.android.volley.Response; 
import com.android.volley.VolleyError; 
import com.android.volley.toolbox.JsonObjectRequest; 
import com.android.volley.toolbox.StringRequest; 
import com.android.volley.toolbox.Volley; 

import org.json.JSONException; 
import org.json.JSONObject; 


import java.util.Scanner; 

public class MainActivity extends AppCompatActivity { 

    TextView tempview; 
    TextView weatherdesc; 
    TextView cityname; 


    Button mButton; 
    EditText mEdit; 
    TextView mText; 
    String currentcity; 
Context context; 
    boolean start=false; 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
      mEdit= (EditText) findViewById(R.id.citylocation); 
     currentcity= mEdit.toString(); 



     tempview= (TextView) findViewById(R.id.temp); 
      weatherdesc=(TextView) findViewById(R.id.Weatherdesc); 





     String url = "http://api.openweathermap.org/data/2.5/weather?q="; 
     url= url + currentcity +"&units=imperial&appid="+API_KEY; 

     JsonObjectRequest jsObjRequest = new JsonObjectRequest 

       (Request.Method.GET, url, null, new Response.Listener<JSONObject>() { 

        @Override 
        public void onResponse(JSONObject response) { 
         tempview.setText("Response: " + response.toString()); 
         weatherdesc.setText("Respnse: " + response.toString()); 
         try { 
          JSONObject Jobject= response.getJSONObject("main"); 
           String tempvalue = Integer.toString((int)Math.round(Jobject.getDouble("temp")/2.8666)); 

          tempview.setText(tempvalue); 



         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } 
       }, new Response.ErrorListener() { 

        @Override 
        public void onErrorResponse(VolleyError error) { 
         // TODO Auto-generated method stub 

        } 
       }); 

// Access the RequestQueue through your singleton class. 
     RequestQueue queue= Volley.newRequestQueue(this); 
     queue.add(jsObjRequest); 
     start=false; 
    } 
}} 
+0

「currentcity = mEdit.toString();」部分在很多方面都是錯誤的。這不是一個控制檯應用程序,我認爲你應該暫停開發這個應用程序,並開始學習Android框架及其基礎知識。然後你可以恢復你離開的地方。 –

+0

是的,@OğuzhanDöngül是正確的.'currentcity = mEdit.getText()。toString();'應該做,而不是在按鈕單擊事件處理程序'button1.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ currentcity = mEdit.getText()。toString(); } });' –

回答

0

如何發送沒有或默認城市名稱的初始請求。首先取用戶的輸入。

在非常基本的術語中,添加一個EditText詢問用戶的城市。 然後將該值分配給cityname變量。添加一個按鈕並附加一個函數,它會在點擊它之後執行,並將其傳遞給一個函數,該函數將構造url並將其發回到onCreate,您可以在其中完成剩餘的處理。

是這樣的:

在onCreate-

button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      currentcity=mEdit.getText().toString(); 
      String url = gotCityName(currentcity;) 
     } 
    }); 

外onCreate-

public String gotCityName(String cityname){ 
String url = "http://api.openweathermap.org/data/2.5/weather?q="; 
     url= url + currentcity +"&units=imperial&appid="+API_KEY; 
return url; 
    } 
+0

初始請求通過默認發送。目前該應用只顯示一個天氣,不知道它是哪個位置,它來自api。我遵循你所說的,但同樣的問題。它沒有提取cityname的第一件事。它顯示默認溫度並保持這種狀態。 –

+0

我不明白。如果您能夠成功地從默認網址中獲得結果,那就很好。之後,您可以在使用Google城市列表進行自動填充建議時向用戶索取城市名稱,然後將該網址永久設置爲符合城市。最後,向API端點發出另一個請求。 –