2017-02-10 34 views
0

我最近開始學習JSON以在我的應用中使用。我發現了一個天氣API(openweathermap.org),並在我的應用程序中使用它。該應用運行良好,但是當我按下按鈕時,沒有任何反應。我的源代碼:我的應用不會將天氣信息添加到TextView(JSON)

import android.content.Context; 
import android.hardware.input.InputManager; 
import android.os.AsyncTask; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import java.io.*; 
import java.net.*; 
import java.util.*; 
import android.util.*; 
import android.view.*; 
import android.view.inputmethod.InputMethodManager; 
import android.widget.*; 
import org.json.*; 


public class MainActivity extends AppCompatActivity { 

    EditText city; 
    TextView weather, description; 

    DownloadTask DownloadTask; 
    public void showWeather (View view) 
    { 
     InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
     imm.hideSoftInputFromWindow(city.getWindowToken(), 0); 

     try 
     { 
      String encodedCityName = URLEncoder.encode(city.getText().toString(), "UTF-8"); 
      DownloadTask = new DownloadTask(); 
      DownloadTask.execute("http://api.openweathermap.org/data/2.5/weather?q=" + encodedCityName + "&appid=812f300ec742971975bbde9a2e0ac0c1"); 
     } 
     catch (UnsupportedEncodingException e) 
     { 
      e.printStackTrace(); 
      Toast.makeText(getApplicationContext(), "Error!", Toast.LENGTH_LONG).show(); 
     } 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     city = (EditText) findViewById(R.id.city); 
     weather = (TextView) findViewById(R.id.weather); 
     description = (TextView) findViewById(R.id.description); 
    } 

    public class DownloadTask extends AsyncTask<String, Void, String> 
    { 
     @Override 
     protected String doInBackground(String... urls) { 
      String result = ""; 
      URL url; 
      HttpURLConnection connection = null; 

      try 
      { 
       url = new URL(urls[0]); 
       connection = (HttpURLConnection) url.openConnection(); 

       InputStream in = connection.getInputStream(); 
       InputStreamReader reader = new InputStreamReader(in); 

       int data = reader.read(); 

       while (data != 1) 
       { 
        char current = (char) data; 
        result += current; 

        data = reader.read(); 
       } 
       return result; 

      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
       Toast.makeText(getApplicationContext(), "Error!", Toast.LENGTH_LONG); 

      } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(String result) { 

      super.onPostExecute(result); 
      Log.i("Weather content", result); 
      try 
      { 
       JSONObject jsonObject = new JSONObject(result); 
       String weatherInfo = jsonObject.getString("weather"); 
       Log.i("Weather Content", weatherInfo); 

       JSONArray jsonArray = new JSONArray(weatherInfo); 

       String getMain = ""; 
       String getDescription = ""; 

       for (int i = 0; i < jsonArray.length(); i++) 
       { 
        JSONObject jsonPart = jsonArray.getJSONObject(i); 

        getMain = jsonPart.getString("main"); 
        getDescription = jsonPart.getString("description"); 

        Log.i("Main", getMain); 

       } 
       if (getMain != "" && getDescription != "") 
       { 
        weather.setText(getMain); 
        description.setText(getDescription); 
       } 
      } 
      catch (JSONException e) 
      { 
       e.printStackTrace(); 
       Toast.makeText(getApplicationContext(), "Error!", Toast.LENGTH_LONG); 
      } 
     } 
    } 

} 

沒有Toasts和Logs工作。

+0

'!getMain.isEmpty()&&!getDescription.isEmpty()' –

+0

@PavneetSingh感謝,但沒有奏效 –

+0

你必須做一些調試,無論是有假設是錯誤'!'敬酒,如果不確保是越來越執行你的任務,並檢查天氣'值content'登錄,並確保您有互聯網的權限和運行權限的棉花糖及以上 –

回答

0

您在讀取數據有錯誤。你應該停止閱讀越來越-1時沒有1

  while (data != -1) 
      { 
       char current = (char) data; 
       result += current; 

       data = reader.read(); 
      } 
+0

'read'函數文檔: https://docs.oracle.com/javase/7/docs/api/java/io/InputStreamReader.html#read() – zed

+0

我總是用它來讀取數據,它的工作原理。這不是問題 –

+0

爲了確保在我的主要源代碼中記錄'result';) – zed