Java 使用makehttp请求的HttpPost

Java 使用makehttp请求的HttpPost,java,android,json,Java,Android,Json,嗨,朋友们,请帮帮我,我从15天开始就罢工了,我找不到我到处找过的,没有解决办法。 这是我用来发出http请求的代码,它在json中传递参数 在下面的字符串中,它将空字符串传递给jurl String jurl=calling.makeHttpRequest(url,“GET”,params) public类loaditems扩展异步任务{ @凌驾 受保护的字符串背景(字符串…字符串){ 字符串url=”http://www.yell4food.com/json/data_standard_it

嗨,朋友们,请帮帮我,我从15天开始就罢工了,我找不到我到处找过的,没有解决办法。 这是我用来发出http请求的代码,它在json中传递参数 在下面的字符串中,它将空字符串传递给
jurl
String jurl=calling.makeHttpRequest(url,“GET”,params)

public类loaditems扩展异步任务{
@凌驾
受保护的字符串背景(字符串…字符串){
字符串url=”http://www.yell4food.com/json/data_standard_item_new.php?rname=standardtakeaway";
List params=new ArrayList();
参数添加(新的BasicNameValuePair(“cname”,项目));
参数添加(新的BasicNameValuePair(“cids”,ID));
调用=新调用();
String jurl=calling.makeHttpRequest(url,“GET”,params);
日志d(“项目”,法律);
试一试{
JSONArray数组=新的JSONArray(jurl);
对于(int i=0;i
打电话,上课 公共类呼叫{

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;

}
静态输入流为=null;
静态JSONObject jObj=null;
静态字符串json=“”;
//建造师
公共电话(){
}
//函数从url获取json
//通过使用HTTP POST或GET方法
公共字符串makeHttpRequest(字符串url、字符串方法、,
列表参数){
//发出HTTP请求
试一试{
//检查请求方法
如果(方法==“POST”){
//请求方法为POST
//defaultHttpClient
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}else if(方法==“GET”){
//请求方法是GET
DefaultHttpClient httpClient=新的DefaultHttpClient();
String paramString=URLEncodedUtils.format(params,“utf-8”);
url+=“?”+参数字符串;
HttpGet HttpGet=新的HttpGet(url);
HttpResponse HttpResponse=httpClient.execute(httpGet);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
is,“iso-8859-1”),8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
json=sb.toString();
}捕获(例外e){
Log.e(“缓冲区错误”,“错误转换结果”+e.toString());
}
//返回JSON字符串
返回json;
}

}

使用截击库…..我编辑了问题,在其中添加了调用类,检查一下是否太高兴了,兄弟,它解决了我的问题,但数据以不同的方式来了,兄弟,我一直尝试了将近15天,兄弟
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"));