我想从远程服务器获得响应。这里是我的代码:在HttpGet的android java.lang.IllegalArgumentException
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)
以下异常:在指数59非法字符查询:http://www.pappico.ru/promo.php?action=RegisterUser¶ms= { “用户id”
java.lang.IllegalArgumentException异常: 1,“userName”:“ЮрийКлинских”}
'{'字符有什么问题?我已阅读有关此异常的一些帖子,并找到了解决方法,但这种方法会导致另一种情况例外:如果我uncommet线url = URLEncoder.encode(url, "UTF-8");
它craches在行response = httpClient.execute(httpGet);
这样的例外:
java.lang.IllegalStateException:目标主机必须不为空或在参数中设置。方案= NULL,主机= NULL,PATH = http://www.pappico.ru/promo.php?action=RegisterUser¶ms= { “用户id”:1, “username” 的: “Юрий+Клинских”}
不知道我应该怎么做,使之工作。任何帮助,将不胜感激:)
这样做并线响应获得例外android.os.NetworkOnMainThreadException = httpClient.execute(HTTPGET); ' – konunger 2013-03-07 11:22:49
在我看来,这是我的错误 - 我正在从UI线程使用网络)) – konunger 2013-03-07 11:30:29