Page 79 - 3-2
P. 79

// function get json from url
                // by making HTTP POST or GET mehtod
                public JSONObject makeHttpRequest(String url, String method,
                                                  List<NameValuePair> params) {


                    // Making HTTP request
                    try {


                        // check for request method
                        if(method == "POST"){


                            // request method is POST
                            // defaultHttpClient
                            DefaultHttpClient httpClient = new DefaultHttpClient();
                            HttpPost httpPost = new HttpPost(url);
                            httpPost.setEntity(new UrlEncodedFormEntity(params));
                            HttpResponse httpResponse = httpClient.execute(httpPost);
                            HttpEntity httpEntity = httpResponse.getEntity();
                            is = httpEntity.getContent();


                        }else if(method == "GET"){
                            // request method is GET
                            DefaultHttpClient httpClient = new DefaultHttpClient();
                            String paramString = URLEncodedUtils.format(params, "utf-8");
                            url += "?" + paramString;
                            HttpGet httpGet = new HttpGet(url);


                            HttpResponse httpResponse = httpClient.execute(httpGet);
                            HttpEntity httpEntity = httpResponse.getEntity();
                            is = httpEntity.getContent();


                        }


                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();


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


                    }


                    try {


                                                         - 79 -
   74   75   76   77   78   79   80   81   82   83   84