android 将获取的网络信息转换为字符串
/**
* 获取json字符串
* */
public String getJsonString(HttpGet get) throws ClientProtocolException,
IOException {
httpClient = new DefaultHttpClient();
// 请求超时
httpClient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
HttpResponse response = httpClient.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
Log.i("jia", "成功");
InputStream in = response.getEntity().getContent();
InputStreamReader reader = new InputStreamReader(in);
BufferedReader bufferedReader = new BufferedReader(reader);
StringBuffer s = new StringBuffer();
String s_buf;
while ((s_buf = bufferedReader.readLine()) != null) {
s.append(s_buf);
}
return s.toString();
} else {
return null;
}
}
class JsonStringTask extends AsyncTask<String, Void, String> {
HttpGet get;
private String s = "wo";
public JsonStringTask(HttpGet get) {
this.get = get;
}
@Override
protected String doInBackground(String... params) {
try {
s = getJsonString(get);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return s;
}
@Override
protected void onPostExecute(String result) {
Log.i("jia", result);
super.onPostExecute(result);
}
}
* 获取json字符串
* */
public String getJsonString(HttpGet get) throws ClientProtocolException,
IOException {
httpClient = new DefaultHttpClient();
// 请求超时
httpClient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
HttpResponse response = httpClient.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
Log.i("jia", "成功");
InputStream in = response.getEntity().getContent();
InputStreamReader reader = new InputStreamReader(in);
BufferedReader bufferedReader = new BufferedReader(reader);
StringBuffer s = new StringBuffer();
String s_buf;
while ((s_buf = bufferedReader.readLine()) != null) {
s.append(s_buf);
}
return s.toString();
} else {
return null;
}
}
class JsonStringTask extends AsyncTask<String, Void, String> {
HttpGet get;
private String s = "wo";
public JsonStringTask(HttpGet get) {
this.get = get;
}
@Override
protected String doInBackground(String... params) {
try {
s = getJsonString(get);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return s;
}
@Override
protected void onPostExecute(String result) {
Log.i("jia", result);
super.onPostExecute(result);
}
}