2016-03-24 12 views
-1

初めてJSON Parsingに手を差し伸べています。私はURLからデータを取得し、Volleyを使用して解析しようとしています。 マイコード:ネストされたJSON Volleyを使ってアンドロイドでURLを解析する

  package com.example.hp.citysearchapp; 

     import android.app.DownloadManager; 
     import android.app.ProgressDialog; 
     import android.content.Intent; 
     import android.support.v7.app.AppCompatActivity; 
     import android.os.Bundle; 
     import android.util.Log; 
     import android.widget.TextView; 
     import android.widget.Toast; 

     import com.android.volley.Request; 
     import com.android.volley.Response; 
     import com.android.volley.VolleyError; 
     import com.android.volley.toolbox.JsonObjectRequest; 
     import org.json.JSONArray; 
     import org.json.JSONException; 
     import org.json.JSONObject; 
     import org.json.JSONException; 
     import org.json.JSONObject; 

     import java.io.File; 
     import java.io.FileInputStream; 
     import java.io.FileNotFoundException; 
     import java.io.IOException; 
     import java.io.InputStream; 

     public class CityDetailsActivity extends AppCompatActivity { 

      public static String url; 
      String getId; 
      TextView tv; 
      private ProgressDialog pDialog; 

      @Override 
      protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.activity_city_details); 
       tv=(TextView)findViewById(R.id.textView); 
       Bundle bundle = getIntent().getExtras(); 
        getId = bundle.getString("gettingId"); 

       url="http://test.maheshwari.org/services/testwebservice.asmx/GetCity?cityId="+getId 

       JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { 
        @Override 
        public void onResponse(JSONObject jsonObject) { 

         try { 

      String citycode = jsonObject.getString("CityCode");  //problem line , no string received here 
          Toast.makeText(CityDetailsActivity.this,citycode,Toast.LENGTH_SHORT).show(); 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } 
       }, new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError volleyError) { 

        } 
       }); 

      } 

      private void hidePDialog() { 
       if (pDialog != null) { 
        pDialog.dismiss(); 
        pDialog = null; 
       } 
      } 
     } 

私はURLから取得していたデータが、このタイプは次のとおりです。

{ 
"CityCode": 374, 
    "State": { 
"StateCode": 29, 
"CountryCode": 1, 
"Country": { 
    "CountryCode": 1, 
    "IsdCode": "+91" 
} 
}, 
"Country": { 
"CountryCode": 1, 
"CountryName": "India", 
"IsdCode": "+91" 
}, 
"GPlaceId": "ChIJgeJXTN9KbDkRCS7yDDrG4Qw", 
"Latitude": 26.912434, 
"Longitude": 75.787271, 
"ActiveStatus": 1 
} 

も変数「国番号を取得する方法、すなわち、ネストされたJSONの解析woth私を助けてください"

+0

Stateオブジェクト内のCountryオブジェクトの内側にあるCountryCode。 – user6111220

+0

'Log.d(" TAG "、jsonObject.toString())' showは、あなたが期待しているのと同じレスポンスを記録していますか? – Sourabh

+0

Log.d()には何も表示されません。「TAG」のログは表示されません。 @Sourabh – user6111220

答えて

0

あなたはhere定義されているようStringRequestを使用し、あなたのリクエストでJSONオブジェクトを送信する必要はありませんので。

RequestQueue queue = Volley.newRequestQueue(this); 
String url ="http://<your URL>"; 

// Request a string response from the provided URL. 
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, 
     new Response.Listener<String>() { 
    @Override 
    public void onResponse(String response) { 

     //I'm basically displaying the skeleton code. You have to put try/catch clauses when required. 
     JSONObject json = new JSONObject(response); 
     Integer cityCode = json.optInt("CityCode"); 
     String cityName = json.optString("CityName"); 
     JSONObject state = json.optJSONObject("State"); 
     String stateName = state.optString("StateName"); 
    } 
}, new Response.ErrorListener() { 
    @Override 
    public void onErrorResponse(VolleyError error) { 
     mTextView.setText("That didn't work!"); 
    } 
}); 
// Add the request to the RequestQueue. 
queue.add(stringRequest); 
+0

CityCode、StateCodeなどのパラメータはどのように取得されますか? – user6111220

+0

私の答えはあなたにjsonレスポンスを得ましたか? –

+0

うん!!私はこの方法で応答を得ています。しかし今、これらのパラメータを取得する方法は? @RuchiraRandana – user6111220

0

私は間違ったボレーがStringのに最適です覚えていない場合、この

String countryCode = jsonObject.getJSONObject("Country").getInt("CountryCode"); 
0

ような何かを行います。あなたはあなたの人生をより簡単にするために、JSONオブジェクトのStringの中ですべての値を作ることができるように、doubleとintの場所を使用しています。

これは私がそれを行う方法で、Stringからアプリケーションに必要なものに変換します。例えば以下を参照してください。

{ 
"CityCode": "374", 
"State": { 
"StateCode": "29", 
"CountryCode": "1", 
"Country": { 
"CountryCode": "1", 
"IsdCode": "+91" 
} 
}, 
"Country": { 
"CountryCode": "1", 
"CountryName": "India", 
"IsdCode": "+91" 
}, 
"GPlaceId": "ChIJgeJXTN9KbDkRCS7yDDrG4Qw", 
"Latitude": "26.912434", 
"Longitude": "75.787271", 
"ActiveStatus": "1" 
} 
関連する問題