编辑:“你好”已被删除。那么,你好。
通过访问URL,我应该得到一个字符串作为响应。所以我在这个网站上借了一部分代码,并采用了一种方法来将我的字符串变成一个变量。不幸的是,跳过了“Try”块,返回的字符串是“ * ”。我对这个剧本有什么误解?
变量“响应”是我应该得到的响应,还是别的什么?
提前致谢。
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;
}
答案 0 :(得分:0)
我不知道这是否正常,但这是你必须做的基本想法
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();
<强>更新强>
这是我的代码的简短剪辑,它没有问题:
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();
}
}
}
顺便说一下缓冲读数是你可以选择的最佳解决方案,但我真的不认为这是你的问题,因为我在HTTPEntity 之前已经注意到它不能为空,所以我假设有HTTPRequest有问题,可能尝试使用fiddler检查请求结果。
答案 1 :(得分:0)
这是我通常使用的(发布而不是你的获取,但应该是相同的)。
public String getMessage(){
InputStream is = null;
String result = "";
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(<url>);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("var1", var1));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
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();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
Log.i("result string", result);
}
}
答案 2 :(得分:0)
这是我们在我们的应用程序中使用的,在获取JSON字符串时非常有用
String response = "";
try {
HttpClient httpClientpost = new DefaultHttpClient();
String postURL = "http://somepostaddress.com";
HttpPost post = new HttpPost(postURL);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("postName1", "value1"));
params.add(new BasicNameValuePair("postName2", "value2"));
params.add(new BasicNameValuePair("postName3", "value3"));
//And So On
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params);
post.setEntity(ent);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
HttpResponse responsePOST = httpClientpost.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
//Server Response
response = EntityUtils.toString(resEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
答案 3 :(得分:0)
我想你正在从UI线程调用它,并且Android正在抛出一个异常,说你不能在主线程上进行网络I / O.您可能没有看到这个,因为您的catch块忽略了异常,e.printStrackTrace()
不会将异常转储到logcat(您必须调用Log.e("tag", "boom", e);
才能在日志中实际看到它。
首先确认你正在点击异常(修复日志记录,和/或重新抛出异常),如果是这样,那么你需要从另一个线程调用它,例如通过进入AsyncTask。
答案 4 :(得分:0)
我只是试图在Activity而不是AsyncTask中执行此操作。现在在doInBackground中工作得更好。谢谢大家。