HTTP请求应该返回一个字符串 [英] HTTP request should return a string
问题描述
编辑:你好已被删除。所以,你好。
通过达成一个网址,我应该得到一个字符串作为回应。所以我借了code的一部分,这个网站上,并在适应的方法让我的字符串转换成一个变量。可惜的是,尝试块被跳过,返回的字符串是 * 的。我有什么误解了一些关于这个剧本?
是变量回应是我应该得到的回应,还是其他什么东西?
先谢谢了。
私人字符串的getMessages(){
字符串响应=***;
尝试{
HttpClient的客户端=新DefaultHttpClient();
字符串的getURL =HTTP://***.com/message/
HTTPGET GET =新HTTPGET(的getURL);
HTT presponse responseGet = client.execute(获取);
HttpEntity resEntityGet = responseGet.getEntity();
如果(resEntityGet!= NULL){
//做一些与响应
响应= EntityUtils.toString(resEntityGet);
Log.i(GET RESPONSE响应);
}
}赶上(例外五){
e.printStackTrace(); } 返回响应;
}
我不知道这是否正常工作,但是这是基本的想法,你必须做什么
StringBuilder的建设者=新的StringBuilder();
InputStream的内容= entity.getContent();
读者的BufferedReader =新的BufferedReader(新的InputStreamReader(内容));
串线;
而((行= reader.readLine())!= NULL){
builder.append(线);
}
串响应= builder.toString();
更新:
这是我的code,这是工作没有问题的简短文档片断:
INT状态code = mResponse.getStatusLine()的getStatus code()。
InputStream为= NULL;
StringBuilder的StringBuilder的=新的StringBuilder();
如果(状态code == HttpStatus.SC_OK){
HttpEntity实体= mResponse.getEntity();
如果(实体!= NULL){
尝试{
是= entity.getContent();
}赶上(IllegalStateException异常五){
e.printStackTrace();
}赶上(IOException异常五){
e.printStackTrace();
}
字节[]缓冲区=新的字节[1024];
INT长;
尝试{
而((长度= is.read(缓冲液))大于0){
stringBuilder.append(新的String(缓冲,0,长度));
}
}赶上(IOException异常五){
e.printStackTrace();
}
}
}
由缓冲读的是你可以选择最佳的解决方案,但我真的不认为这就是你的问题,因为我已经在HTTPEntity 不能为空,所以我想有前注意方式有毛病的HTT prequest,也许尝试使用招来检查请求的结果。
Edit: "hello" has been deleted. So, hello.
By reaching an URL, I should get a string as response. So I borrowed a part of code on this website and adapted in a method to get my string into a variable. Unfortunatelly, the "Try" block is skipped and the returned string is "*". What do I have misunderstood something about this script ?
Is the variable "response" what I should get as a response, or something else ?
Thanks in advance.
private String getMessages() {
String response = "***";
try {
HttpClient client = new DefaultHttpClient();
String getURL = "http://***.com/message/";
HttpGet get = new HttpGet(getURL);
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null) {
// do something with the response
response = EntityUtils.toString(resEntityGet);
Log.i("GET RESPONSE", response);
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
I don't know if this works correctly but this is the basic idea what you must do
StringBuilder builder = new StringBuilder();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
String response = builder.toString();
UPDATE:
This is a short snipped of my code, which is working without problems:
int statusCode = mResponse.getStatusLine().getStatusCode();
InputStream is = null;
StringBuilder stringBuilder = new StringBuilder();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity entity = mResponse.getEntity();
if (entity != null) {
try {
is = entity.getContent();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
byte[] buffer = new byte[1024];
int length;
try {
while ((length = is.read(buffer)) > 0) {
stringBuilder.append(new String(buffer, 0, length));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
by the way the buffered reading is the best solution you can choose, but I dont really think thats your problem, as I have noticed before the HTTPEntity must not be null, so I assume there is something wrong with the HTTPRequest, maybe try to check the request-results using fiddler.
这篇关于HTTP请求应该返回一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!