0

I am trying to parse a JSON to a URL onclick of item from listview and using the post request

I am able to parse the JSON from URL variable but other URL where i am trying to make a post request is where I am stuck with POST request

Any IDEAS


[After Edit]

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);

                }
            });
        }

    }

}

RestaurantDesc.java

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();
                }   
            }
        }
    }
}

[AFTER - EDIT]

[Log]

08-29 10:48:34.931: D/dalvikvm(499): GC_EXTERNAL_ALLOC freed 171K, 48% free 3078K/5895K, external 2111K/2137K, paused 89ms
08-29 10:48:36.621: W/System.err(499): org.json.JSONException: Value Cannot of type java.lang.String cannot be converted to JSONObject
08-29 10:48:36.621: W/System.err(499):  at org.json.JSON.typeMismatch(JSON.java:107)
08-29 10:48:36.621: W/System.err(499):  at org.json.JSONObject.<init>(JSONObject.java:158)
08-29 10:48:36.651: W/System.err(499):  at org.json.JSONObject.<init>(JSONObject.java:171)
08-29 10:48:36.681: W/System.err(499):  at com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:114)
08-29 10:48:36.821: W/System.err(499):  at com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:1)
08-29 10:48:36.891: W/System.err(499):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-29 10:48:37.042: W/System.err(499):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-29 10:48:37.152: W/System.err(499):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-29 10:48:37.263: W/System.err(499):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-29 10:48:37.263: W/System.err(499):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-29 10:48:37.263: W/System.err(499):  at java.lang.Thread.run(Thread.java:1019)
08-29 10:48:37.272: D/AndroidRuntime(499): Shutting down VM
08-29 10:48:37.272: W/dalvikvm(499): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-29 10:48:37.302: E/AndroidRuntime(499): FATAL EXCEPTION: main
08-29 10:48:37.302: E/AndroidRuntime(499): java.lang.NullPointerException
08-29 10:48:37.302: E/AndroidRuntime(499):  at com.project.findmybuffet.RestaurantDesc$ParsingAsync.onPostExecute(RestaurantDesc.java:138)
08-29 10:48:37.302: E/AndroidRuntime(499):  at com.project.findmybuffet.RestaurantDesc$ParsingAsync.onPostExecute(RestaurantDesc.java:1)
08-29 10:48:37.302: E/AndroidRuntime(499):  at android.os.AsyncTask.finish(AsyncTask.java:417)
08-29 10:48:37.302: E/AndroidRuntime(499):  at android.os.AsyncTask.access$300(AsyncTask.java:127)
08-29 10:48:37.302: E/AndroidRuntime(499):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
08-29 10:48:37.302: E/AndroidRuntime(499):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-29 10:48:37.302: E/AndroidRuntime(499):  at android.os.Looper.loop(Looper.java:123)
08-29 10:48:37.302: E/AndroidRuntime(499):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-29 10:48:37.302: E/AndroidRuntime(499):  at java.lang.reflect.Method.invokeNative(Native Method)
08-29 10:48:37.302: E/AndroidRuntime(499):  at java.lang.reflect.Method.invoke(Method.java:507)
08-29 10:48:37.302: E/AndroidRuntime(499):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-29 10:48:37.302: E/AndroidRuntime(499):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-29 10:48:37.302: E/AndroidRuntime(499):  at dalvik.system.NativeStart.main(Native Method)
08-29 10:52:25.271: W/ActivityThread(533): Application com.project.findmybuffet is waiting for the debugger on port 8100...
08-29 10:52:25.321: I/System.out(533): Sending WAIT chunk
08-29 10:52:26.061: I/dalvikvm(533): Debugger is active
08-29 10:52:26.171: I/System.out(533): Debugger has connected
08-29 10:52:26.171: I/System.out(533): waiting for debugger to settle...
08-29 10:52:26.381: I/System.out(533): waiting for debugger to settle...
08-29 10:52:26.595: I/System.out(533): waiting for debugger to settle...
08-29 10:52:26.801: I/System.out(533): waiting for debugger to settle...
08-29 10:52:27.009: I/System.out(533): waiting for debugger to settle...
08-29 10:52:27.211: I/System.out(533): waiting for debugger to settle...
08-29 10:52:27.411: I/System.out(533): waiting for debugger to settle...
08-29 10:52:27.611: I/System.out(533): waiting for debugger to settle...
08-29 10:52:27.811: I/System.out(533): waiting for debugger to settle...
08-29 10:52:28.076: I/System.out(533): waiting for debugger to settle...
08-29 10:52:28.289: I/System.out(533): waiting for debugger to settle...
08-29 10:52:28.502: I/System.out(533): waiting for debugger to settle...
08-29 10:52:28.714: I/System.out(533): waiting for debugger to settle...
08-29 10:52:28.927: I/System.out(533): waiting for debugger to settle...
08-29 10:52:29.142: I/System.out(533): waiting for debugger to settle...
08-29 10:52:29.401: I/System.out(533): debugger has settled (1535)
08-29 10:52:38.532: W/ActivityThread(562): Application com.project.findmybuffet is waiting for the debugger on port 8100...
08-29 10:52:38.562: I/System.out(562): Sending WAIT chunk
08-29 10:52:38.631: I/dalvikvm(562): Debugger is active
08-29 10:52:38.772: I/System.out(562): Debugger has connected
08-29 10:52:38.772: I/System.out(562): waiting for debugger to settle...
08-29 10:52:38.972: I/System.out(562): waiting for debugger to settle...
08-29 10:52:39.191: I/System.out(562): waiting for debugger to settle...
08-29 10:52:39.392: I/System.out(562): waiting for debugger to settle...
08-29 10:52:39.592: I/System.out(562): waiting for debugger to settle...
08-29 10:52:39.792: I/System.out(562): waiting for debugger to settle...
08-29 10:52:39.992: I/System.out(562): waiting for debugger to settle...
08-29 10:52:40.201: I/System.out(562): waiting for debugger to settle...
08-29 10:52:40.402: I/System.out(562): waiting for debugger to settle...
08-29 10:52:40.616: I/System.out(562): waiting for debugger to settle...
08-29 10:52:40.822: I/System.out(562): waiting for debugger to settle...
08-29 10:52:41.022: I/System.out(562): waiting for debugger to settle...
08-29 10:52:41.266: I/System.out(562): debugger has settled (1419)
08-29 10:54:39.342: W/ActivityThread(724): Application com.project.findmybuffet is waiting for the debugger on port 8100...
08-29 10:54:39.402: I/System.out(724): Sending WAIT chunk
08-29 10:54:39.421: I/dalvikvm(724): Debugger is active
08-29 10:54:39.441: I/System.out(724): Debugger has connected
08-29 10:54:39.491: I/System.out(724): waiting for debugger to settle...
08-29 10:54:39.691: I/System.out(724): waiting for debugger to settle...
08-29 10:54:39.892: I/System.out(724): waiting for debugger to settle...
08-29 10:54:40.091: I/System.out(724): waiting for debugger to settle...
08-29 10:54:40.302: I/System.out(724): waiting for debugger to settle...
08-29 10:54:40.501: I/System.out(724): waiting for debugger to settle...
08-29 10:54:40.701: I/System.out(724): waiting for debugger to settle...
08-29 10:54:40.902: I/System.out(724): waiting for debugger to settle...
08-29 10:54:41.101: I/System.out(724): waiting for debugger to settle...
08-29 10:54:41.311: I/System.out(724): waiting for debugger to settle...
08-29 10:54:41.511: I/System.out(724): waiting for debugger to settle...
08-29 10:54:41.711: I/System.out(724): waiting for debugger to settle...
08-29 10:54:41.921: I/System.out(724): waiting for debugger to settle...
08-29 10:54:42.121: I/System.out(724): waiting for debugger to settle...
08-29 10:54:42.321: I/System.out(724): waiting for debugger to settle...
08-29 10:54:42.521: I/System.out(724): waiting for debugger to settle...
08-29 10:54:44.291: W/ActivityThread(749): Application com.project.findmybuffet is waiting for the debugger on port 8100...
08-29 10:54:44.312: I/System.out(749): Sending WAIT chunk
08-29 10:54:44.341: I/dalvikvm(749): Debugger is active
08-29 10:54:44.611: I/System.out(749): Debugger has connected
08-29 10:54:44.611: I/System.out(749): waiting for debugger to settle...
08-29 10:54:44.811: I/System.out(749): waiting for debugger to settle...
08-29 10:54:45.021: I/System.out(749): waiting for debugger to settle...
08-29 10:54:45.221: I/System.out(749): waiting for debugger to settle...
08-29 10:54:45.421: I/System.out(749): waiting for debugger to settle...
08-29 10:54:45.621: I/System.out(749): waiting for debugger to settle...
08-29 10:54:45.831: I/System.out(749): waiting for debugger to settle...
08-29 10:54:46.031: I/System.out(749): waiting for debugger to settle...
08-29 10:54:46.237: I/System.out(749): waiting for debugger to settle...
08-29 10:54:46.441: I/System.out(749): waiting for debugger to settle...
08-29 10:54:46.641: I/System.out(749): waiting for debugger to settle...
08-29 10:54:46.856: I/System.out(749): waiting for debugger to settle...
08-29 10:54:47.116: I/System.out(749): waiting for debugger to settle...
08-29 10:54:47.328: I/System.out(749): waiting for debugger to settle...
08-29 10:54:47.541: I/System.out(749): waiting for debugger to settle...
08-29 10:54:47.753: I/System.out(749): waiting for debugger to settle...
08-29 10:54:47.971: I/System.out(749): debugger has settled (1483)
08-29 11:18:47.661: D/dalvikvm(749): GC_EXPLICIT freed 143K, 50% free 2819K/5575K, external 2031K/2137K, paused 92ms
08-29 11:19:53.471: D/dalvikvm(2570): GC_EXTERNAL_ALLOC freed 177K, 48% free 3078K/5895K, external 2111K/2137K, paused 130ms
08-29 11:19:55.421: W/System.err(2570): org.json.JSONException: Value Cannot of type java.lang.String cannot be converted to JSONObject
08-29 11:19:56.012: W/System.err(2570):     at org.json.JSON.typeMismatch(JSON.java:107)
08-29 11:19:56.012: W/System.err(2570):     at org.json.JSONObject.<init>(JSONObject.java:158)
08-29 11:19:56.022: W/System.err(2570):     at org.json.JSONObject.<init>(JSONObject.java:171)
08-29 11:19:56.022: W/System.err(2570):     at com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:114)
08-29 11:19:56.022: W/System.err(2570):     at com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:1)
08-29 11:19:56.022: W/System.err(2570):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-29 11:19:56.022: W/System.err(2570):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-29 11:19:56.031: W/System.err(2570):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-29 11:19:56.031: W/System.err(2570):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-29 11:19:56.031: W/System.err(2570):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-29 11:19:56.031: W/System.err(2570):     at java.lang.Thread.run(Thread.java:1019)
08-29 11:19:56.031: D/AndroidRuntime(2570): Shutting down VM
08-29 11:19:56.031: W/dalvikvm(2570): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-29 11:19:56.052: E/AndroidRuntime(2570): FATAL EXCEPTION: main
08-29 11:19:56.052: E/AndroidRuntime(2570): java.lang.NullPointerException
08-29 11:19:56.052: E/AndroidRuntime(2570):     at com.project.findmybuffet.RestaurantDesc$ParsingAsync.onPostExecute(RestaurantDesc.java:138)
08-29 11:19:56.052: E/AndroidRuntime(2570):     at com.project.findmybuffet.RestaurantDesc$ParsingAsync.onPostExecute(RestaurantDesc.java:1)
08-29 11:19:56.052: E/AndroidRuntime(2570):     at android.os.AsyncTask.finish(AsyncTask.java:417)
08-29 11:19:56.052: E/AndroidRuntime(2570):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
08-29 11:19:56.052: E/AndroidRuntime(2570):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
08-29 11:19:56.052: E/AndroidRuntime(2570):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-29 11:19:56.052: E/AndroidRuntime(2570):     at android.os.Looper.loop(Looper.java:123)
08-29 11:19:56.052: E/AndroidRuntime(2570):     at android.app.ActivityThread.main(ActivityThread.java:3683)
08-29 11:19:56.052: E/AndroidRuntime(2570):     at java.lang.reflect.Method.invokeNative(Native Method)
08-29 11:19:56.052: E/AndroidRuntime(2570):     at java.lang.reflect.Method.invoke(Method.java:507)
08-29 11:19:56.052: E/AndroidRuntime(2570):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-29 11:19:56.052: E/AndroidRuntime(2570):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-29 11:19:56.052: E/AndroidRuntime(2570):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

1

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

于 2013-08-29T06:04:45.113 回答
0

你必须找到你的身份证:

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);

            }
        });

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

于 2013-08-29T05:40:52.820 回答