HttpPost使用make http请求

时间:2015-11-04 06:06:24

标签: java android json

朋友们,请帮助我,我从15天开始,我无法找到我在任何地方都没有找到解决方案。 这是我用来制作http请求的代码,它在json中传递参数 在下面的字符串中,它将空字符串传递给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 公共课致电{

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;

}

}

1 个答案:

答案 0 :(得分:1)

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