我写的代码如下:
public class HttpTest {
public static void main(String[] args) throws IOException {
String formStatusData="[{formState:处理中"+"}]";
DefaultHttpClient httpClient = new DefaultHttpClient();
StringEntity params =new StringEntity(formStatusData,"UTF-8");
List<NameValuePair> list=URLEncodedUtils.parse(params);
System.out.println(URLEncodedUtils.format(list, HTTP.UTF_8);//输出结果为空
String url="http://localhost/seeyon/mattersApplication/mattersApplication.do?method=newMatters&formStatusData="+URLEncodedUtils.format(list, HTTP.UTF_8);
HttpPost request=new HttpPost(url);
try {
request.addHeader("content-type", "application/json");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
int statusCode= response.getStatusLine().getStatusCode();
if(statusCode==HttpStatus.SC_OK){
System.out.println("hahah");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
但是代码我总是感觉有问题,请大家给一个实例代码吧!谢谢了
9 个解决方案
#1
发送json参数,首先你的构造这样的json字符串,你这明显不是json格式的字符串嘛
#2
以流 的形式写、
#3
我已经得到了Json参数,这里只是一个例子而已,我想要问的是如何发送
#4
怎么写,能否举个例子呢?
#5
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair(name, userName));
nvps.add(new BasicNameValuePair(pass, password));
httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
nvps.add(new BasicNameValuePair(name, userName));
nvps.add(new BasicNameValuePair(pass, password));
httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
#6
我需要的不是这种形式,我已经解决了,Thanks,我把它当做字符串发送就行了
#7
List<NameValuePair> params = new ArrayList<NameValuePair>();
StringEntity myEntity = new StringEntity("<commond><code>"+clientVo.getCode()+"</code><data>"+clientVo.getData()+"</data></commond>", "UTF-8");
httppost.setEntity(myEntity);
HttpResponse response = httpclient.execute(httppost);
StringEntity myEntity = new StringEntity("<commond><code>"+clientVo.getCode()+"</code><data>"+clientVo.getData()+"</data></commond>", "UTF-8");
httppost.setEntity(myEntity);
HttpResponse response = httpclient.execute(httppost);
#8
楼主这个问题怎么解决的?如何使用httpClient发送一个字符串?望指点!
#9
请楼主贴一下代码,最近也在搞这个,谢谢
#1
发送json参数,首先你的构造这样的json字符串,你这明显不是json格式的字符串嘛
#2
以流 的形式写、
#3
我已经得到了Json参数,这里只是一个例子而已,我想要问的是如何发送
#4
怎么写,能否举个例子呢?
#5
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair(name, userName));
nvps.add(new BasicNameValuePair(pass, password));
httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
nvps.add(new BasicNameValuePair(name, userName));
nvps.add(new BasicNameValuePair(pass, password));
httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
#6
我需要的不是这种形式,我已经解决了,Thanks,我把它当做字符串发送就行了
#7
List<NameValuePair> params = new ArrayList<NameValuePair>();
StringEntity myEntity = new StringEntity("<commond><code>"+clientVo.getCode()+"</code><data>"+clientVo.getData()+"</data></commond>", "UTF-8");
httppost.setEntity(myEntity);
HttpResponse response = httpclient.execute(httppost);
StringEntity myEntity = new StringEntity("<commond><code>"+clientVo.getCode()+"</code><data>"+clientVo.getData()+"</data></commond>", "UTF-8");
httppost.setEntity(myEntity);
HttpResponse response = httpclient.execute(httppost);
#8
楼主这个问题怎么解决的?如何使用httpClient发送一个字符串?望指点!
#9
请楼主贴一下代码,最近也在搞这个,谢谢