在我的程序中jsonobect和jsonarray没有运行?

时间:2017-11-05 14:55:24

标签: java android json

在我的计划中parseJsonData方法正在运行,但jsonObjectjsonArray未执行。我无法获得输出。

package com.example.rajesh_pc.jsonapplication;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
    ListView listView;
    ProgressDialog progressDialog;
    String url="http://demo.cogzideltemplates.com/anan/mobile/posts/viewall_Posts";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView= (ListView) findViewById(R.id.fruitsList);
        progressDialog=new ProgressDialog(this);
        progressDialog.setMessage("Loadiing");
        progressDialog.show();
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String string) {
                parseJsonData(url);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getApplicationContext(), "Some error occurred!!", Toast.LENGTH_SHORT).show();
                progressDialog.dismiss();
            }
        });

        RequestQueue requestQueue= Volley.newRequestQueue(MainActivity.this);
        requestQueue.add(request);
    }
    void parseJsonData(String jsonstring)
    {

        try {
            System.out.println("executinh parsejson method");
            JSONObject jsonObject = new JSONObject(jsonstring);
            System.out.println("executinh jsonobj method");
            JSONArray jsonArray=jsonObject.getJSONArray("caption");
            System.out.println("executinh jsonarraay method");
            System.out.println("output===>"+jsonArray);
            ArrayList arrayList=new ArrayList();
            for (int i=0;i<jsonArray.length();i++)
            {
                arrayList.add(getString(i));
            }
            ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.activity_list_item,arrayList);
            listView.setAdapter(arrayAdapter);

        } catch (JSONException e) {
            e.printStackTrace();
        }
progressDialog.dismiss();

    }
}

0 个答案:

没有答案