2017-06-13 134 views
-2

我已经尝试了第一个JSON部分本身没有wcurrentobject,它完美的作品。但是因为我尝试添加另一个wcurrrent对象,我似乎无法使其工作。这里的URL 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()); 
 

 
} 
 

 

 
}

+0

这里有没有JavaScript的。请尽可能标记为狭义。 – tadman

回答

0

这是因为在里面feelslike_c当前的JSONObject

这里您的JSON数据格式为http://www.jsoneditoronline.org/

{ 
    "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"); 
+0

感谢人,顺便说一句,是目前和条件2不同的对象?或者它们是对象内的对象 – Karanvir1

+0

'Condition'是'Current'对象内的对象。更多细节可以在这里阅读:http://www.json.org/ –

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){ 
} 
相关问题