初めて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私を助けてください"
Stateオブジェクト内のCountryオブジェクトの内側にあるCountryCode。 – user6111220
'Log.d(" TAG "、jsonObject.toString())' showは、あなたが期待しているのと同じレスポンスを記録していますか? – Sourabh
Log.d()には何も表示されません。「TAG」のログは表示されません。 @Sourabh – user6111220