以下是显示json
以下数据的新闻。在导航菜单中但未获取。
在menuitem
[{"news_cat_id":"118DFDD8_7E4B_FCE6_CFF3_48CCF0269680","cat_name":"Desh","cat_code":"Desh","cat_url":"Desh","cat_title":"Desh","cat_keyword":"Desh","cat_desc":"Desh","cat_dependency":"0","galli_display":"0","highlight":"0","cat_position":"1","cat_subposition":"0","status":"0","Time":"2016-03-03 06:44:42"},{"news_cat_id":"2A129D1A_DD90_09A6_B585_51189B2C3DB9","cat_name":"Mumbai","cat_code":"Mumbai","cat_url":"Mumbai","cat_title":"Mumbai","cat_keyword":"Mumbai","cat_desc":"Mumbai","cat_dependency":"0","galli_display":"0","highlight":"0","cat_position":"2","cat_subposition":"0","status":"0","Time":"2016-03-03 06:45:14"}
这是一个获取城市名称frpm json数据,但应用程序崩溃了..
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
ArrayList<List_items> list_items_arr = new ArrayList<List_items>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
new GetContacts().execute();
}
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class GetContacts extends AsyncTask<String, Void , String > {
protected void onPreExecute() {
super.onPreExecute();
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Please wait...");
progressDialog.setCancelable(false);
progressDialog.show();
}
protected String doInBackground(String... params)
{
String url = "http://elitesolution.co.in/galli/menu.php";
ServiceHandler serviceHandler = new ServiceHandler();
String jsonstr = serviceHandler.makeServiceCall(url, ServiceHandler.POST);
Log.d("Response:", ">" + jsonstr);
try {
JSONObject jsonObject = new JSONObject(jsonstr);
JSONArray contacts = jsonObject.getJSONArray(jsonstr);
// JSONArray jsonArray=new JSONArray(jsonstr);
for(int i=0;i<contacts.length();i++)
{
JSONObject c=contacts.getJSONObject(i);
String city=c.getString("cat_url");
Toast.makeText(MainActivity.this, "" + city, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
return jsonstr;
}
} public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int list_items = item.getItemId();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
答案 0 :(得分:0)
尝试解析你的json响应:
JsonArray jsonArray=new JsonArray("Your json string");
for(int i=0;i<jsonArray.length();i++)
{
JsonObject jsonObject=jsonArray.getJsonObject(i);
String city=jsonObject.getString("cat_url");
}
答案 1 :(得分:0)
首先创建一个arraylist来存储你的数据
ArrayList<List_items> list_items_arr = new ArrayList<List_items>();
为Lisr_items创建类
public class List_items{
String item_id,item_name;
public void setItem_id(String item_id) {
this.item_id = item_id;
}
public void setItem_name(String item_name) {
this.item_name = item_name;
}
}
从服务器获取响应并解析它
JsonArray jsonArray=new JsonArray("Your Json Responce");
for(int i=0;i<items.length();i++){
JSONObject list_obj=items.getJSONObject(i);
List_items list_items = new List_items();
list_items.setItem_name(list_obj.getString("cat_url"));
list_items.setItem_id(list_obj.getString("news_cat_id"));
list_items_arr .add(list_items);
}
答案 2 :(得分:0)
试试这个:
JsonArray jsonArray=new JsonArray("Your json responce");
for(int i=0;i<jsonArray.length();i++)
{
JsonObject jsonObject=jsonArray.getJsonObject(i);
String city=jsonObject.getString("cat_url");
Toast.makeText(this, ""+city, Toast.LENGTH_LONG).show();
}