2017-07-11 9 views
1

私はアンドロイドスタジオが初めてです。簡単なアプリケーションでURLからJSONデータを取得しようとしています。つまり、Android StudioのVolleyでサーバーからJSONデータを取得します。私は私のデバイス上でデバッグするときに、それが正常に動作しますが、任意のデータを表示しないと、私は間違って何かがあると確信しています私のアプリはURLからJSONデータを表示していません

私のコード:

package imo.meteoiraq; 

import android.app.DownloadManager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
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.Volley; 

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

public class MainActivity extends AppCompatActivity { 
RequestQueue rq; 
TextView nameText,descriptionText,fbUrlText,youtubeUrlTxt; 
    int humidity; 
    int wind_speed; 
    int temperature; 
    String url="/stationlookupstation=I1410&units=metric&v=2.0&format=json"; 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     rq= Volley.newRequestQueue(this); 

     nameText= (TextView) findViewById(R.id.tempText); 
     descriptionText = (TextView) findViewById(R.id.wind_speed); 
     fbUrlText = (TextView) findViewById(R.id.humidity); 
     sendjsonrequest(); 
    } 
public void sendjsonrequest(){ 
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { 
     @Override 
     public void onResponse(JSONObject response) { 
      try { 
       temperature = response.getInt("temperature"); 
       wind_speed= response.getInt("wind_speed"); 
       humidity= response.getInt("type"); 

       nameText.setText(temperature); 
       descriptionText.setText(humidity); 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 

     } 
    }); 
    rq.add(jsonObjectRequest); 
} 
} 

私のデータのJSON:

{ 
    "stations": { 
    "I1410": { 
     "updated": 1499668936, 
     "ageh": 0, 
     "agem": 0, 
     "ages": 58, 
     "type": "PWS", 
     "wind_dir_degrees": 315, 
     "wind_speed": 6.1, 
     "wind_gust_speed": 11.1, 
     "humidity": 10, 
     "temperature": 40.5, 
     "precip_rate": null, 
     "precip_today": 0, 
     "pressure": 1002.26, 
     "dewpoint": null, 
     "windchill": null 
    } 
    } 
} 
+0

「何か間違っている」あなたは正確に何かを見つけなければなりません。あなたはlogcatに例外のスタックトレースを取得しますか? 'catch(JSONException e){ e.printStackTrace();で何かを出力するかもしれません。 } ' –

答えて

2

最初にJsonObjectステーションとI1410を取得し、次にage = I1410JO.getInt( "age")を使用します。

JSONObject stationsJO = response.getJSONObject("stations"); 
JSONObject I1410JO = stationsJO.getJSONObject("I1410"); 
int ages = I1410JO.getInt("ages"); 
+0

APP STOP WORKING – aligassan

+0

show stacktrace –

+0

アンドロイドモンターを意味しますか? – aligassan

0

あなたのURLにエラーがあり、正しい(GETまたはPOST)メソッドで正しいURLを修正してください。

1

プログラムには2つの問題があります。

Rasoul MiriがステーションJSONオブジェクトにアクセスし、次にI1410JO JSONオブジェクトにアクセスしたことについて既に説明しました。

第2の問題は、温度が整数形式ではなく、2倍形式であることです。そのため、温度はI1410JO.getDouble( "温度")を使用して取得する必要があります。

関連する問題