Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HttpGet中的android java.lang.IllegalArgumentException_Java_Android_Httpclient - Fatal编程技术网

HttpGet中的android java.lang.IllegalArgumentException

HttpGet中的android java.lang.IllegalArgumentException,java,android,httpclient,Java,Android,Httpclient,我正在尝试从远程服务器获取响应。这是我的密码: private static String baseRequestUrl = "http://www.pappico.ru/promo.php?action="; @SuppressWarnings("deprecation") public String executeRequest(String url) { String res = ""; HttpClient httpClient = ne

我正在尝试从远程服务器获取响应。这是我的密码:

private static String baseRequestUrl = "http://www.pappico.ru/promo.php?action=";

    @SuppressWarnings("deprecation")
    public String executeRequest(String url) {
        String res = "";
        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse response;      

        try {   
            //url = URLEncoder.encode(url, "UTF-8");
            HttpGet httpGet = new HttpGet(url);

            response = httpClient.execute(httpGet);
            Log.d("MAPOFRUSSIA", response.getStatusLine().toString());

            HttpEntity entity = response.getEntity();

            if (entity != null) {
                InputStream inStream = entity.getContent();
                res = streamToString(inStream);

                inStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return res; 
    }

    public String registerUser(int userId, String userName) {
        String res = "";
        String request = baseRequestUrl + "RegisterUser&params={\"userId\":" +
                 userId + ",\"userName\":\"" + userName + "\"}";

        res = executeRequest(request);

        return res; 
    }
我在第
HttpGet-HttpGet=newhttpget(url)
行中得到了以下异常:

java.lang.IllegalArgumentException:索引59处的查询中的非法字符:{“userId”:1,“userName”:“ЮЮЮЮЮЮЮЮЮЮЮЮЮ107

“{”字符有什么问题?我已经阅读了一些关于此异常的帖子,并找到了一个解决方案,但此解决方案会导致另一个异常:如果我取消了行
url=urlcoder.encode(url,“UTF-8”);
它在行
response=httpClient.execute(httpGet);
出现此异常:

java.lang.IllegalStateException:目标主机不能为null,也不能在参数中设置。scheme=null,host=null,path={“userId”:1,“userName”:“Юцц+ццццццццц1094


我不知道该怎么做才能让它工作。如果有任何帮助,我们将不胜感激:)

您必须对URL参数进行编码:

String request = baseRequestUrl + "RegisterUser&params=" +    
        java.net.URLEncoder.encode("{\"userId\":" + userId + ",\"userName\":\"" + userName + "\"}", "UTF-8");
尝试:

(这将URL片段编码为params=…),而不是整个URL


奖金: 请注意,JSON通常是通过POST(而不是GET)传输的。您可以使用类似“Live Headers”的程序,手动执行步骤(例如注册用户)以查看幕后发生的情况。在这种情况下,您将在实体体中发送{..}信息。以下是一种方法-

另外,编写JSON的另一种方法(特别是当它变得更复杂时)是使用模型类,然后使用ObjectMapper(如Jackson)将其转换为字符串。这很方便,因为您可以避免在字符串中使用类似\“的格式


这里有一些这样的例子:

这样做并在line response=httpClient.execute(httpGet)处获取异常android.os.NetworkOnMainThreadException;`在我看来这是我的错误-我正在从UI线程处理网络)
public String registerUser(int userId, String userName) {
        String res = "";

        String json = "{\"userId\":" +
                 userId + ",\"userName\":\"" + userName + "\"}";
        String encodedJson = URLEncoder.encode(json, "utf-8");

        String request = baseRequestUrl + "RegisterUser&params=" + encodedJson;

        res = executeRequest(request);
        return res;
    }