我正在学习Android,我正在研究如何触发请求并从服务器接收结果。我正在做的是
// Button Click Event
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String testURL = "http://djs-corner.appspot.com/getClosestClubs?lat=40.7600624&lon=-73.98558";
// Create http GET method
HttpClient client = new DefaultHttpClient();
// Create http GET method
HttpGet getTest = new HttpGet(testURL);
// Fire a request
try {
HttpResponse response = client.execute(getTest);
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
String result = convertStreamToString(is);
Log.e("RESULT", result);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("ClientProtocolException is ", e.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("IOException is ", e.toString());
}
}
});
String convertStreamToString(InputStream inputStream) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
sb.append(line + "\n");
return sb.toString();
}
我回来的结果只是原始结果的一部分。请查看here处的图片。如果我们看一下上面网址中的数据,我们可以看到一些数据被砍掉了。 我在这里做错了什么。