我有一个问题,当我们想要将json请求发布到服务器时,它总是返回 org.apache.http.conn.EofSensorInputStream@44f4a1d0 当我们要打印响应时,那么什么是这种反应的手段?
代码:
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
HttpResponse response;
JSONObject json = new JSONObject();
try{
HttpPost post = new HttpPost("http://122.180.114.68/eqixmobile/siteservice/um/ibx");
json.put("username", ed_usrName.getText().toString());
json.put("password", ed_pass.getText().toString());
post.setHeader("Content-Type", "application/json");
post.setHeader("Accept", "application/json");
StringEntity se = new StringEntity( "credentials: " + json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
response = client.execute(post);
/*Checking response */
if(response!=null){
InputStream in = response.getEntity().getContent();
//String str = response.getEntity().getContent().toString();//Get the data in the entity
System.out.println("This is service response:"+in);
}
else
{
System.out.println("This is no any service response:");
}
}
catch(Exception e){
e.printStackTrace();
//createDialog("Error", "Cannot Estabilish Connection");
}
答案 0 :(得分:3)
您从服务器获得的响应是在inputstream中,因此将其转换为字符串然后使用它。等,
String result=null;
InputStream is = entity.getContent();
// convert stream to string
result = convertStreamToString(is);
result = result.replace("\n", "");
public static String convertStreamToString(InputStream is) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
return sb.toString();
}
答案 1 :(得分:0)
我建议使用此方法来获取json响应
获取方法:
public class HttpManager {
public static String getData(String uri) {
BufferedReader reader = null;
try {
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
}
}
对于Post方法:
public class HttpRequest {
public static String getData(String uri, String params) {
BufferedReader reader = null;
try {
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(
con.getOutputStream());
writer.write(params);
writer.flush();
StringBuilder sb = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
Log.i("exception", "" + e.getMessage());
return null;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
}
}