通过GET方法发送值

Sha*_*zeb 6 android

我一直在尝试通过GET方法将数据发送到服务器,但我无法找到办法.我在异步任务中尝试了很少的代码,但没有.web服务是在cakePhp中制作的,格式如下:

Base_URI/users/add.json?json={“email”: xxx@x.com, “password”: “xxxxxxxxx”, “first_name”: “Xyz”, “last_name”: “Xyz”}
Run Code Online (Sandbox Code Playgroud)

要求Android专家找到摆脱这个问题的方法.谢谢

这是代码:

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;
Run Code Online (Sandbox Code Playgroud)

HttpGet不接受这种格式的url并且给出错误但是当我在浏览器上尝试它时它工作正常.错误如下:

Illegal character in query at index 56
Run Code Online (Sandbox Code Playgroud)

Sha*_*zeb 3

最后,经过一整天的调试和尝试不同的解决方案,我解决了问题:)

我需要对参数部分而不是整个 URL 进行编码,如下所示:

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

感谢各位的支持 !