1

I'm trying to get a simple url string returned from a java servlet in android. I am able to get the url back but there is a null prepended to it.

Here is the code from Android I use to call the servlet:

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 

Here is the servlet code:

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

Here is what the url looks like:

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

Is this the right way to do this? I tried Gson but it chokes on the null also.

Thanks in advance.

4

1 回答 1

2
    output = "";
    while ((line = rd.readLine()) != null) {
        output += line + System.getProperty("line.separator");
    }

By the way, The best practice is

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();
于 2012-05-08T03:44:49.577 回答