如何将Java HttpPost对象显示为字符串?
我正在Android中创建一个如何将Java HttpPost对象显示为字符串?,java,android,http-post,Java,Android,Http Post,我正在Android中创建一个HttpPost对象,以便与客户端操作的服务器通信。不幸的是,服务器没有向我们提供非常有用的错误消息;我希望看到HttpPost对象的内容是一个字符串,这样我就可以将它发送给我们的客户,客户可以将它与他期望的内容进行比较 如何将HttpPost对象转换为一个字符串,以反映它到达服务器时的外观?我通常以这种方式发布(服务器答案是JSON对象): postData是: public static String postData(String url, JSONObjec
HttpPost
对象,以便与客户端操作的服务器通信。不幸的是,服务器没有向我们提供非常有用的错误消息;我希望看到HttpPost
对象的内容是一个字符串,这样我就可以将它发送给我们的客户,客户可以将它与他期望的内容进行比较
如何将HttpPost对象转换为一个字符串,以反映它到达服务器时的外观?我通常以这种方式发布(服务器答案是JSON对象): postData是:
public static String postData(String url, JSONObject obj) {
// Create a new HttpClient and Post Header
HttpClient httpclient = null;
try {
HttpParams myParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(myParams, 30000);
HttpConnectionParams.setSoTimeout(myParams, 30000);
httpclient = new DefaultHttpClient(myParams);
} catch (Exception e) {
Log.e("POST_DATA", "error in httpConnection");
e.printStackTrace();
}
InputStream is = null;
try {
HttpPost httppost = new HttpPost(url.toString());
//Header here httppost.setHeader();
StringEntity se = new StringEntity(obj.toString());
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
// // Do something with response...
is = entity.getContent();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// convert response to string
BufferedReader reader = null;
String result = null;
try {
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
} finally {
try {
if (reader != null)
reader.close();
if (is != null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (result != null) {
try {
@SuppressWarnings("unused")
JSONObject jObjec = new JSONObject(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
}
return result;
}
希望它能帮上忙。实际上,我用
NameValuePair
做了httppost
。。。。。。我正在显示用于执行httppost
,然后将响应转换为字符串的代码
请参见以下方法代码:
public String postData(String url, String xmlQuery) {
final String urlStr = url;
final String xmlStr = xmlQuery;
final StringBuilder sb = new StringBuilder();
Thread t1 = new Thread(new Runnable() {
public void run() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(urlStr);
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("xml", xmlStr));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
Log.d("Vivek", response.toString());
HttpEntity entity = response.getEntity();
InputStream i = entity.getContent();
Log.d("Vivek", i.toString());
InputStreamReader isr = new InputStreamReader(i);
BufferedReader br = new BufferedReader(isr);
String s = null;
while ((s = br.readLine()) != null) {
Log.d("YumZing", s);
sb.append(s);
}
Log.d("Check Now",sb+"");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Getting from Post Data Method "+sb.toString());
return sb.toString();
}
publicstringpostdata(stringurl,stringxmlquery){
最后一个字符串urlStr=url;
最终字符串xmlStr=xmlQuery;
最终StringBuilder sb=新StringBuilder();
线程t1=新线程(新的可运行线程(){
公开募捐{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(urlStr);
试一试{
List nameValuePairs=新的ArrayList(1);
添加(新的BasicNameValuePair(“xml”,xmlStr));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
Log.d(“Vivek”,response.toString());
HttpEntity=response.getEntity();
InputStream i=entity.getContent();
Log.d(“Vivek”,i.toString());
InputStreamReader isr=新的InputStreamReader(i);
BufferedReader br=新的BufferedReader(isr);
字符串s=null;
而((s=br.readLine())!=null){
Log.d(“YumZing”,s);
某人追加;
}
Log.d(“立即检查”,sb+”);
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
});
t1.start();
试一试{
t1.join();
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
System.out.println(“从Post数据方法获取”+sb.toString());
使某人返回字符串();
}
应该在执行后使用它
public static String httpPostToString(HttpPost httppost) {
StringBuilder sb = new StringBuilder();
sb.append("\nRequestLine:");
sb.append(httppost.getRequestLine().toString());
int i = 0;
for(Header header : httppost.getAllHeaders()){
if(i == 0){
sb.append("\nHeader:");
}
i++;
for(HeaderElement element : header.getElements()){
for(NameValuePair nvp :element.getParameters()){
sb.append(nvp.getName());
sb.append("=");
sb.append(nvp.getValue());
sb.append(";");
}
}
}
HttpEntity entity = httppost.getEntity();
String content = "";
if(entity != null){
try {
content = IOUtils.toString(entity.getContent());
} catch (Exception e) {
e.printStackTrace();
}
}
sb.append("\nContent:");
sb.append(content);
return sb.toString();
}
您的服务器代码使用哪种语言?是Servlet(或)PHP(或)其他东西吗?.NET,但我无法控制服务器;这是我感兴趣的电话,但还是谢谢你。@Andrewyld不介意,但我想你错过了我说的我正在使用NamePairValue进行HTTP Post的那部分
public static String httpPostToString(HttpPost httppost) {
StringBuilder sb = new StringBuilder();
sb.append("\nRequestLine:");
sb.append(httppost.getRequestLine().toString());
int i = 0;
for(Header header : httppost.getAllHeaders()){
if(i == 0){
sb.append("\nHeader:");
}
i++;
for(HeaderElement element : header.getElements()){
for(NameValuePair nvp :element.getParameters()){
sb.append(nvp.getName());
sb.append("=");
sb.append(nvp.getValue());
sb.append(";");
}
}
}
HttpEntity entity = httppost.getEntity();
String content = "";
if(entity != null){
try {
content = IOUtils.toString(entity.getContent());
} catch (Exception e) {
e.printStackTrace();
}
}
sb.append("\nContent:");
sb.append(content);
return sb.toString();
}