我正在尝试从远程服务器获得响应。这是我的代码:
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¶ms={\"userId\":" +
userId + ",\"userName\":\"" + userName + "\"}";
res = executeRequest(request);
return res;
}
我得到以下异常HttpGet httpGet = new HttpGet(url)
:
java.lang.IllegalArgumentException:索引 59 处查询中的非法字符:http: //www.pappico.ru/promo.php ?action=RegisterUser¶ms= {"userId":1,"userName":"Юрий Клинских"}
'{' 字符有什么问题?我已经阅读了一些有关此异常的帖子并找到了解决方案,但是此解决方案会导致另一个异常:如果我取消注释行url = URLEncoder.encode(url, "UTF-8");
,它会在response = httpClient.execute(httpGet);
出现此类异常的情况下出现:
java.lang.IllegalStateException:目标主机不能为空,或在参数中设置。方案=null,主机=null,路径= http://www.pappico.ru/promo.php?action=RegisterUser¶ms= {"userId":1,"userName":"Юрий+Клинских"}
不知道我该怎么做才能让它工作。任何帮助,将不胜感激:)