简体   繁体   中英

HttpPost using make http request

hi friends please help me i had struck from 15days i am not able to find the i had searched everywhere no solution for that. this is code i using to make http request where its pass the parameter in the json In this below string its passing the null string to jurl String jurl=calling.makeHttpRequest(url, "GET", params);

public class loaditems extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... strings) {
        String url="http://www.yell4food.com/json/data_standard_item_new.php?rname=standardtakeaway";
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("cname", item));
        params.add(new BasicNameValuePair("cids",ids));
        Calling calling=new Calling();
        String jurl=calling.makeHttpRequest(url, "GET", params);
        Log.d("items", jurl);

        try {
            JSONArray array = new JSONArray(jurl);
            for (int i = 0; i < array.length(); i++) {
                JSONObject first = array.getJSONObject(i);
                JSONParser parser = new JSONParser();
                parser.setMenuname(first.getString("menu_name"));
                JSONArray getitems = first.getJSONArray("items");
                for (int j = 0; j < getitems.length(); j++) {
                    JSONObject sitems = getitems.getJSONObject(j);
                    parser.setIid(sitems.getInt("id"));
                    parser.setBaseName(sitems.getString("BaseName"));
                    parser.setItemdesc(sitems.getString("itemdesc"));
                    JSONArray subitems = sitems.getJSONArray("subitems");
                    for (int l = 0; l < subitems.length(); l++) {
                        JSONObject thrid = subitems.getJSONObject(l);
                        parser.setSid(thrid.getInt("id"));
                        parser.setSubItemdesc(thrid.getString("SubItemdesc"));
                        parser.setSubItemprice(thrid.getString("SubItemprice"));
                    }
                    itemsdata.add(parser);
                }
                secondAdapter.notifyDataSetChanged();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        Toast.makeText(getApplicationContext(),"Data Loaded", Toast.LENGTH_SHORT).show();
        TextView textView= (TextView) findViewById(R.id.textView);
        textView.setText(item);
    }
}

Calling.Class public class Calling {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public Calling() {

}

// function get json from url
// by making HTTP POST or GET mehtod
public String 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 {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // return JSON String
    return json;

}

}

See the code below

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




line  url += "?" + paramString; 


adding a query string separator '?' in your url.
but in a valid url only query string separator '?' is allowed but you already include that one in your url

Try this solution
change your url from
 String url="http://www.yell4food.com/json/data_standard_item_new.php?rname=standardtakeaway";

to`enter code here`

String url="http://www.yell4food.com/json/data_standard_item_new.php";

and pass parameter rname in NameValuePairs list like this

    params.add(new BasicNameValuePair("rname", "standardtakeaway"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM