Android Http 从 Servlet 获取字符串数据

标签 android string google-app-engine http servlets

我正在尝试获取从 android 中的 java servlet 返回的简单 url 字符串。我能够取回 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);
    }
}

这是 url 的样子:

nullhttp://anyusers-MacBook-Air.local:8888/_ah/upload/ag5tY2hlY2stcGF5bWVudHIbCxIVX19CbG9iVXBsb2FkU2Vzc2lvbl9fGBwM

这样做正确吗?我尝试了 Gson,但它也被 null 阻塞了。

提前致谢。

最佳答案

    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();

关于Android Http 从 Servlet 获取字符串数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10492238/

相关文章:

c++ - 在 char 数组中存储和恢复一组单词

google-app-engine - GAE : What's faster loading an include config file from GCS or from cloud SQL

android - 如何在android中裁剪图像?

Android应用程序闪烁

java - 图像未通过位图从我的画廊上传

android - ScrollView 内 TextView 中的超链接不可单击

Javascript 搜索子字符串

c - 将字符数组作为参数传递(C 内核代码)

javascript - 从 html 页面上的 javascript 函数调用 python 方法来获取数据

google-app-engine - 谷歌应用引擎 ndb : using id or key name?