我不明白为什么这不会打印到日志

时间:2017-06-13 00:50:10

标签: java android json

我已经尝试了第一个json部分,没有wcurrentobject,它完美地工作。但是,因为我尝试添加另一个wcurrrent对象,我似乎无法让它工作。这是api“http://api.apixu.com/v1/current.json?key=76d62bf509e64633a4970055170706&q=Victoria

的网址

我正在尝试打印“feelslike_c”键值

package com.karanvir.again;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    EditText city;
    Button go;
    TextView resultofw;
    String text;
    DownloadTask task;
    TextView loadings;
    String temp;
    TextView displaytemp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        city=(EditText) findViewById(R.id.editText);
        go=(Button) findViewById(R.id.button);
        setSupportActionBar(toolbar);
loadings=(TextView) findViewById(R.id.textView2);
        resultofw=(TextView) findViewById(R.id.textView);
displaytemp=(TextView) findViewById(R.id.textView3);


    }

    public class DownloadTask extends AsyncTask<String,Void,String> {
        @Override
        protected String doInBackground(String... urls) {
            String result = "";
            URL url;
            HttpURLConnection urlConnection = null;
            try {
                url = new URL(urls[0]);
                urlConnection = (HttpURLConnection) url.openConnection();
                InputStream in = urlConnection.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();

            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
//conver result into json object
            try{
                JSONObject jsonObject=new JSONObject(result);

                //get current JSONObject from result JSONObject
                JSONObject currentJSONObject = jsonObject.getJSONObject("current");
                JSONObject wcurrentJSONObject = jsonObject.getJSONObject("current");

                //Now get condition JSONObject from current JSONObject
                JSONObject conditionJSONObject = currentJSONObject.getJSONObject("condition");
                JSONObject wconditionJSONObject = wcurrentJSONObject.getJSONObject("condition");

                text = conditionJSONObject.getString("text");
                temp = wconditionJSONObject.getString("feelslike_c");

                //Now print condition JSONObject
                Log.i("website content", "condition json object : " + text.toString());
                Log.i("website temp", "condition json object : " + temp.toString());
                JSONArray arr= new JSONArray(conditionJSONObject.toString());




            } catch (Exception e){

            }
            resultofw.setText(text);
            displaytemp.setText(temp);


        }
    }

public  void click(View view){
    task=new DownloadTask();
    task.execute("http://api.apixu.com/v1/current.json?key=76d62bf509e64633a4970055170706&q=" + city.getText().toString());

}


}

2 个答案:

答案 0 :(得分:0)

这是因为当前 JSONObject

中的feelslike_c

此处使用http://www.jsoneditoronline.org/

格式化您的JSON数据
{
  "location": {
    "name": "Victoria",
    "region": "British Columbia",
    "country": "Canada",
    "lat": 48.43,
    "lon": -123.35,
    "tz_id": "America/Vancouver",
    "localtime_epoch": 1497317643,
    "localtime": "2017-06-12 18:34"
  },
  "current": {
    "last_updated_epoch": 1497317406,
    "last_updated": "2017-06-12 18:30",
    "temp_c": 16.3,
    "temp_f": 61.3,
    "is_day": 1,
    "condition": {
      "text": "Sunny",
      "icon": "//cdn.apixu.com/weather/64x64/day/113.png",
      "code": 1000
    },
    "wind_mph": 10.5,
    "wind_kph": 16.9,
    "wind_degree": 250,
    "wind_dir": "WSW",
    "pressure_mb": 1014,
    "pressure_in": 30.4,
    "precip_mm": 0,
    "precip_in": 0,
    "humidity": 49,
    "cloud": 0,
    "feelslike_c": 16.3,
    "feelslike_f": 61.3,
    "vis_km": 15.8,
    "vis_miles": 9
  }
}

所以,改为使用:

temp = wconditionJSONObject.getString("feelslike_c"); 

您应该使用wcurrentJSONObject

temp = wcurrentJSONObject.getString("feelslike_c"); 

答案 1 :(得分:0)

试试这个,

try {

    JSONObject jsonObject=new JSONObject(result);

    JSONObject currentJSONObject = jsonObject.getJSONObject("current");
    JSONObject conditionJSONObject = currentJSONObject.getJSONObject("condition");

    String text = conditionJSONObject.getString("text");
    String feelslike_c = currentJSONObject.getString("feelslike_c");

    Log.i("website content", "text: " + text);
    Log.i("website temp", "feelslike_c: " + feelslike_c);

} catch (Exception e){
}