Closed. This question needs debugging details。它当前不接受答案。












想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。

4年前关闭。





我有一个Json数据,我试图解析为android。但是我遇到了JSONException;-没有类别的值。有人帮我

我的MainActivity在这里:

public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {

//private static String TAG = MainActivity.class.getSimpleName();

private Toolbar mToolbar;
private FragmentDrawer drawerFragment;
String data1,data2,data3,data4,data5;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mToolbar = (Toolbar) findViewById(R.id.toolbar);

    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    drawerFragment = (FragmentDrawer)
            getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
    drawerFragment.setDrawerListener(this);
    // display the first navigation drawer view on app launch
    displayView(0);
    Log.d("oncreate method","");

    new ProductsAsynTask().execute("http://opencart.codeniques.com/myshop/?route=feed/web_api/product&id=30&key=test123");


}

public class ProductsAsynTask extends AsyncTask<String,Void,Void>{

    ProgressDialog dialog;

    protected void onPreExecute(){
        super.onPreExecute();
        Log.d("In onPreExceute","");
        dialog = new ProgressDialog(MainActivity.this);
        dialog.setMessage("Loading, Please wait");
        dialog.setTitle("Connecting server");
        dialog.show();
        dialog.setCancelable(false);
    }

    protected Void doInBackground(String... param){
        try{
            Log.d("In doInBackground","");

            HttpClient client= new DefaultHttpClient();
            HttpPost post = new HttpPost(param[0]);
            HttpResponse response = client.execute(post);

            int status = response.getStatusLine().getStatusCode();

            if(status == 200){
                Log.d("Status",""+status);

                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);

                JSONObject jsonObject = new JSONObject(data);
                JSONArray jArray = jsonObject.getJSONArray("categories");

                for(int i = 0;i < jArray.length();i++){

                    JSONObject jsonObject1 = jArray.getJSONObject(i);

                    data1 = jsonObject1.getString("name");
                    Log.d("hello ",data1);

                    JSONObject jsonObject2 = jsonObject1.getJSONObject("children");

                    data2 = jsonObject2.getString("name");
                    Log.d("hello2 ",data2);

                    JSONObject jsonObject3 = jsonObject2.getJSONObject("children_lv3");

                    data3 = jsonObject3.getString("name");
                    Log.d("hello3 ",data3);
                    data4 = jsonObject3.getString("href");
                    Log.d("hello4 ",data4);

                    data5=jsonObject2.getString("href");
                    Log.d("hello5 ",data5);
                }

            }
        }catch(IOException e){
            Log.e("Error IOException :",e.getMessage());
        }catch (JSONException e){
            Log.e("Error JSONException",e.getMessage());
        }
            return null;
    }

    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        dialog.dismiss();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
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;
    }

    if(id == R.id.action_search){
        Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onDrawerItemSelected(View view, int position) {
    displayView(position);
}

private void displayView(int position) {
    Fragment fragment = null;
    String title = getString(R.string.app_name);
    switch (position) {
        case 0:

           fragment = new HomeFragment();
           title = getString(R.string.title_home);
            break;
        case 1:
            fragment = new FriendsFragment();
            title = getString(R.string.title_friends);
            break;
        case 2:
            fragment = new MessagesFragment();
            title = getString(R.string.title_messages);
            break;
        default:
            break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container_body, fragment);
        fragmentTransaction.commit();

        // set the toolbar title
        getSupportActionBar().setTitle(title);
    }
}


}

我是android开发的新手,请帮助我。提前致谢!

最佳答案

这是因为您的JSON数据不包含categories JSONArray
所以先检查它是这样的:-

if(jsonObject.has("categories"))
                {
                    JSONArray jArray = jsonObject.getJSONArray("categories");
 for(int i = 0;i < jArray.length();i++){

                    JSONObject jsonObject1 = jArray.getJSONObject(i);

                    data1 = jsonObject1.getString("name");
                    Log.d("hello ",data1);

                    JSONObject jsonObject2 = jsonObject1.getJSONObject("children");

                    data2 = jsonObject2.getString("name");
                    Log.d("hello2 ",data2);

                    JSONObject jsonObject3 = jsonObject2.getJSONObject("children_lv3");

                    data3 = jsonObject3.getString("name");
                    Log.d("hello3 ",data3);
                    data4 = jsonObject3.getString("href");
                    Log.d("hello4 ",data4);

                    data5=jsonObject2.getString("href");
                    Log.d("hello5 ",data5);
                }
                }

看到JSON时,请先检查您的JSONException;- No value for数据

07-24 22:10