通过GET方法发送的值 [英] Send values by GET method

查看:115
本文介绍了通过GET方法发送的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图通过GET方法的数据发送到服务器,但我无法找到一个方法来做到这一点。我曾尝试一些codeS的异步任务,但一无所获。 Web服务是由在CakePHP中,格式是这样的:

  Base_URI /用户/ add.json JSON = {电子邮件:xxx@x.com,密码:XXXXXXXXX,FIRST_NAME:XYZ,姓氏:XYZ}
 

Android的专家被要求想法子出这个问题。谢谢

下面是code:

 名单,其中,的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>();
            nameValuePairs.add(新BasicNameValuePair(电子邮件,UserDummy.email));
            nameValuePairs.add(新BasicNameValuePair(密码,UserDummy.password));
            nameValuePairs.add(新BasicNameValuePair(FIRST_NAME,UserDummy.fname));
            nameValuePairs.add(新BasicNameValuePair(姓氏,UserDummy.lname));
            //使HTTP请求
    尝试 {

        //检查请求的方法
        如果(方法==POST){
            //请求方法是POST
            // defaultHttpClient
            DefaultHttpClient的HttpClient =新DefaultHttpClient();
            HttpPost httpPost =新HttpPost(URL);
            httpPost.setEntity(新UrlEn codedFormEntity(PARAMS));

            HTT presponse HTT presponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = HTT presponse.getEntity();
            是= httpEntity.getContent();

        }否则,如果(方法==GET){
            //请求方法是GET
            DefaultHttpClient的HttpClient =新DefaultHttpClient();
            字符串中的paramString = URLEn codedUtils.format(参数,可以HTTP.UTF_8);
            ?JSON = URL + = {+ +的paramString}; ;
            HTTPGET HTTPGET =新HTTPGET(URL);

            HTT presponse HTT presponse = httpClient.execute(HTTPGET);
            HttpEntity httpEntity = HTT presponse.getEntity();
            是= httpEntity.getContent();
        }

    }赶上(UnsupportedEncodingException E){
        e.printStackTrace();
        Log.v(XXX,e.getMessage());
    }赶上(ClientProtocolException E){
        e.printStackTrace();
        Log.v(XXX,e.getMessage());
    }赶上(IOException异常E){
        e.printStackTrace();
        Log.v(XXX,e.getMessage());
    }

    尝试 {
        的BufferedReader读卡器=新的BufferedReader(新的InputStreamReader(
                是,ISO-8859-1),8);
        StringBuilder的SB =新的StringBuilder();
        串线= NULL;
        而((行= reader.readLine())!= NULL){
            sb.append(行+\ N);
        }
        is.close();
        JSON = sb.toString();
    }赶上(例外五){
        Log.e(缓冲区错误,转换的结果错误+ e.toString());
    }

    //尝试解析字符串到一个JSON对象
    尝试 {
        jObj =新的JSONObject(JSON);
    }赶上(JSONException E){
        Log.e(JSON解析器,错误分析数据+ e.toString());
    }

    //返回JSON字符串
    返回jObj;
 

HTTPGET不接受URL这种格式,并提供了错误,但是当我尝试在浏览器中正常工作。错误是以下内容:

 在指数56非法字符查询
 

解决方案

最后调试,并尝试不同的解决方案了整整一天后,我解决了这个问题:)

我需要连接code中的部分参数,而不是整个URL是这样的:

 字符串URL =Base_URI /用户/ add.json JSON =?;
    URL = URL + URLEn coder.en code({\电子邮件\:\+电子邮件+\,\密码\:\+密码+\}, UTF-8);
 

谢谢大家的支持!

I have been trying to send data to server through GET method but I am unable to find a way to do it. I have tried few codes in asynchronous task but nothing. The web services are made in cakePhp and the format is like this:

Base_URI/users/add.json?json={"email": xxx@x.com, "password": "xxxxxxxxx", "first_name": "Xyz", "last_name": "Xyz"}

Android experts are requested to figure a way out of this problem. Thanks

Here is the code:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("email", UserDummy.email));
            nameValuePairs.add(new BasicNameValuePair("password", UserDummy.password));
            nameValuePairs.add(new BasicNameValuePair("first_name", UserDummy.fname));
            nameValuePairs.add(new BasicNameValuePair("last_name", UserDummy.lname));
            // 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, HTTP.UTF_8);
            url += "?json={" + paramString+"}";                                                                                                                                                     ;
            HttpGet httpGet = new HttpGet(url);

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

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        Log.v("XXX", e.getMessage());
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        Log.v("XXX", e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        Log.v("XXX", e.getMessage());
    }

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

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

HttpGet is not accepting this format of url and is giving the error but when I try it on browser it works fine. Error is following:

Illegal character in query at index 56

解决方案

Finally after debugging and trying different solutions the whole day, I solved the problem :)

I needed to encode the parameters part and not the whole URL like this:

String url = "Base_URI/users/add.json?json=";
    url =url +  URLEncoder.encode("{\"email\":\""+email+"\",\"password\":\""+password+"\"}", "UTF-8");

Thanks everyone for your support !

这篇关于通过GET方法发送的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆