我正在尝试从android中的java servlet返回一个简单的url字符串。我能够恢复网址但是前面有一个空值。
以下是我用来调用servlet的Android代码:
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://localhost:8888/mcheck_gen_url_fu");
HttpResponse response = client.execute(request);
//Here i try to read the response
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
while ((line = rd.readLine()) != null) {
output += line + System.getProperty("line.separator");
}
//I am able to get the response but there is a null pre-pended
这是servlet代码:
public class mcheck_gen_url_fu extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
String output = blobstoreService.createUploadUrl("/mcheck_file_upload");
resp.setContentType("text/plain");
resp.getWriter().print(output);
}
}
以下是网址的内容:
nullhttp://anyusers-MacBook-Air.local:8888 / _ah /上传/ ag5tY2hlY2stcGF5bWVudHIbCxIVX19CbG9iVXBsb2FkU2Vzc2lvbl9fGBwM
这是正确的方法吗?我尝试了Gson,但它也在null上窒息。
提前致谢。
答案 0 :(得分:2)
output = "";
while ((line = rd.readLine()) != null) {
output += line + System.getProperty("line.separator");
}
顺便说一句,最佳做法是
final StringBuilder sb = new StringBuilder();
final String sep = System.getProperty("line.separator");
while ((line = rd.readLine()) != null) {
sb.append(line).append(sep);
}
output = sb.toString();