Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 通过对服务器的请求解析JSON 我试图从listview中单击一个项目并使用post请求将JSON解析为URL_Android - Fatal编程技术网

Android 通过对服务器的请求解析JSON 我试图从listview中单击一个项目并使用post请求将JSON解析为URL

Android 通过对服务器的请求解析JSON 我试图从listview中单击一个项目并使用post请求将JSON解析为URL,android,Android,我能够从URL变量解析JSON,但我试图发出post请求的其他URL就是我被post请求卡住的地方 有什么想法吗 [编辑后] MainActivity.java public class MainActivity extends Activity { // url to make request private static String url = "http://54.218.73.244:7003/"; private static String url1 =

我能够从
URL
变量解析JSON,但我试图发出post请求的其他URL就是我被post请求卡住的地方

有什么想法吗


[编辑后] MainActivity.java

public class MainActivity extends Activity {

    // url to make request
    private static String url = "http://54.218.73.244:7003/";

    private static String url1 = "http://54.218.73.244:7003/RestaurantDesc/";

    TextView timedisplay;
    ListView yourListView;

    String item;
    private HashMap<Integer, String> TimeMap = new HashMap<Integer, String>();

    List<Item> yourData = new ArrayList<Item>();

    ProgressDialog progressDialog;

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


        timedisplay=(TextView) findViewById(R.id.RestaurantTimeID);
        yourListView = (ListView) findViewById(R.id.listViewID);



        //Instantiating ProgressDialog with onCreate method
        progressDialog=new ProgressDialog(MainActivity.this);
        new ParsingAsync().execute();

    }

    private class ParsingAsync extends AsyncTask<Void, Void, Void>
    {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog=ProgressDialog.show(MainActivity.this, "", "Please Wait", true, false);


        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub


            String _response = null;
            try
            {
            HttpClient httpclient = new DefaultHttpClient();
            httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
            HttpGet request = new HttpGet(url);
            HttpResponse response = httpclient.execute(request);
            HttpEntity resEntity = response.getEntity();
             _response =EntityUtils.toString(resEntity);
            JSONObject jsonObject = new JSONObject(_response);
            JSONArray first_array = jsonObject.getJSONArray("restaurants");
            JSONArray second_array = jsonObject.getJSONArray("RestaurantTimings");
             for(int i=0;i<first_array.length();i++)
             {
                    JSONObject c = second_array.getJSONObject(i);
                    Item item = new Item();

                    // Storing each json item in variable
                    int id = c.getInt("_id");
                    String TIME = c.getString("RestaurantTime");
                    item.setTime(TIME);

                    c = first_array.getJSONObject(i);

                    String NAME=c.getString("restaurantNAME");
                    item.setName(NAME);

                    String URL=c.getString("url");
                    item.setUrl(URL);


                    yourData.add(item);
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;

        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            progressDialog.dismiss();


            ListAdapter customAdapter = new ListAdapter(MainActivity.this, R.layout.itemlistrow, yourData);
            yourListView.setAdapter(customAdapter);
            yourListView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    item = yourData.get(position).getName();


                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost(url1);

                    try {
                        // Add your data
                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                        nameValuePairs.add(new BasicNameValuePair("Key", item));
                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                        // Execute HTTP Post Request
                        HttpResponse response = httpclient.execute(httppost);

                    } catch (ClientProtocolException e) {
                        // TODO Auto-generated catch block
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                    }


                    Intent i = new Intent(MainActivity.this, RestaurantDesc.class);
                    i.putExtra("key", url1);
                    //i.putExtra("CC_RES",item.toString());
                    startActivity(i);

                }
            });
        }

    }

}
public class RestaurantDesc extends Activity{
    // url to make request


    String url ;
    ProgressDialog progressDialog;
    JSONObject jsonObject;
    JSONArray first_array ;
    JSONArray second_array;
    TextView textView;
    TextView text;
    TextView topdisp;

    private SparseArray<String> startarsMap = new SparseArray<String>();
    private SparseArray<String> saladsMap = new SparseArray<String>();
    private SparseArray<String> maincourseMap = new SparseArray<String>();
    private SparseArray<String> desertMap = new SparseArray<String>();


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

        url = getIntent().getStringExtra("key");
        progressDialog=new ProgressDialog(RestaurantDesc.this);
        new ParsingAsync().execute();
    }




    private class ParsingAsync extends AsyncTask<Void, Void, Void>
    {



        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog=ProgressDialog.show(RestaurantDesc.this, "", "Please Wait", true, false);

            topdisp=(TextView) findViewById(R.id.TopNavigationBarRestaurantDescActivityName);
            Button BACKBUTTON=(Button) findViewById(R.id.TopNavigationBarRestaurantDescActivityBackButton);
            BACKBUTTON.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent emp1=new Intent(RestaurantDesc.this,MainActivity.class);
                    startActivity(emp1);
                }
            });


            Button PHOTOBUTTON=(Button) findViewById(R.id.RestaurantPhotosButton);
            PHOTOBUTTON.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent pht=new Intent(RestaurantDesc.this,RestaurantPhotos.class);
                    pht.putExtra("key", getIntent().getStringExtra("key"));

                    startActivity(pht); 
                }
            });



        }



        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
            String _response = null;
            try
            {
                HttpClient httpclient = new DefaultHttpClient();
                httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
                HttpGet request = new HttpGet(url);
                HttpResponse response = httpclient.execute(request);
                HttpEntity resEntity = response.getEntity();
                _response =EntityUtils.toString(resEntity);

                jsonObject = new JSONObject(_response);
                first_array = jsonObject.getJSONArray("RestaurantNAME");


            } catch (JSONException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;

        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            progressDialog.dismiss();

            //TextView timedisplay=(TextView) findViewById(R.id.RestaurantTimeID);

            for(int i=0; i < first_array.length(); i++)
            {
                try {
                    JSONObject detail_obj = first_array.getJSONObject(                  


                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }   
            }
        }
    }
}
公共类MainActivity扩展活动{
//发出请求的url
专用静态字符串url=”http://54.218.73.244:7003/";
专用静态字符串url1=”http://54.218.73.244:7003/RestaurantDesc/";
文本视图时间显示;
ListView你的ListView;
字符串项;
私有HashMap TimeMap=新HashMap();
列出您的数据=新建ArrayList();
进行对话进行对话;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timedisplay=(TextView)findViewById(R.id.RestaurantTimeID);
yourListView=(ListView)findViewById(R.id.listViewID);
//使用onCreate方法实例化ProgressDialog
progressDialog=新建progressDialog(MainActivity.this);
新建ParsingAsync().execute();
}
私有类ParsingAsync扩展异步任务
{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
progressDialog=progressDialog.show(MainActivity.this,“,”请稍候“,true,false);
}
@凌驾
受保护的Void doInBackground(Void…参数){
//TODO自动生成的方法存根
字符串_response=null;
尝试
{
HttpClient HttpClient=新的DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);
HttpGet请求=新的HttpGet(url);
HttpResponse response=httpclient.execute(请求);
HttpEntity当前性=response.getEntity();
_response=EntityUtils.toString(resEntity);
JSONObject JSONObject=新的JSONObject(_响应);
JSONArray first_array=jsonObject.getJSONArray(“餐厅”);
JSONArray second_array=jsonObject.getJSONArray(“restaurantmings”);

对于(int i=0;i您必须找到您的id:

TextView timedisplay=(TextView) findViewById(R.id.RestaurantTimeID);

ListView yourListView = (ListView) findViewById(R.id.listViewID);
在onCreate()方法上:

并仅在onPostExecute()方法中设置适配器:

并加上:

yourListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                item = yourData.get(position).getName();


                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url1);

                try {
                    // Add your data
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    nameValuePairs.add(new BasicNameValuePair("Key", item));
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    // Execute HTTP Post Request
                    HttpResponse response = httpclient.execute(httppost);

                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                }


                Intent i = new Intent(MainActivity.this, RestaurantDesc.class);
                i.putExtra("key", url1);
                //i.putExtra("CC_RES",item.toString());
                startActivity(i);

            }
        });
yourListView.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
item=yourData.get(position.getName();
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url1);
试一试{
//添加您的数据
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“键”,项));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
意向i=新意向(MainActivity.this,RestaurantDesc.class);
i、 putExtra(“键”,url1);
//i、 putExtra(“CC_RES”,item.toString());
星触觉(i);
}
});

在oncreate()方法上找到listView的id并将其从onPostExecute()中删除后方法:

首先检查您的JSON数据,然后尝试解析它…我认为您在解析JSON对象时遇到了JSOnError,请检查第2行的logcat错误并尝试解决它…

请查看更新的问题…我做了您要求我做的更改…我得到了上面的错误日志…任何想法…您还删除了MCL吗单击onPostExecute()方法中的listview事件,并将其添加到after onCreate()中方法..Httppost方法..是否正确..就像我正在调用服务器并更新url..这些操作在我的代码中是合法的?我正在检测在POST请求后从服务器收到的JSON解析错误..我将检查并查看..**还**添加到after onCreate()方法的意思是…就像在Oncreate方法中一样?您是否得到了该链接的响应。
yourListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                item = yourData.get(position).getName();


                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url1);

                try {
                    // Add your data
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    nameValuePairs.add(new BasicNameValuePair("Key", item));
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    // Execute HTTP Post Request
                    HttpResponse response = httpclient.execute(httppost);

                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                }


                Intent i = new Intent(MainActivity.this, RestaurantDesc.class);
                i.putExtra("key", url1);
                //i.putExtra("CC_RES",item.toString());
                startActivity(i);

            }
        });