客服端与服务器端JSON数据交互的POST传输:
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了客服端与服务器端JSON数据交互的POST传输:相关的知识,希望对你有一定的参考价值。
post请求客户端端代码:
1.
public void String LoginByPost(String username,String pwd){
String path = "";
URL url = new URL(paht);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);//请求超时
conn.setRequestMehtod("POST");
//准备数据
String data ="username=" + username +"&pwd=" + pwd;
conn.setRequestProperty("Content-Type","application/x-form-urlencoded");
conn.setRequestProperty("Content-Length", data.length + "");
conn.setDoOutput(true); //是否允许向外写数据
OputStream os = conn.getOutputStream();
os.write(data.getByte());
int code = conn.getResponseCode();
if(code == 200){
InputStream is = conn.getInputStream();
String text = StreamTool.readInputStream(is);
return text;
}else{
return null;
}
}
2.
public void String LoginClientPost(String username, String pwd){
HttpClient client = new DefultHttpClient();
String path = "";
HttpPost = post = new HttpPost(path);
List<NameValuePair> paimer = new ArrayList<NameValuePair>();
paimer.add(new BasicNameValuePair("username",username));
post.setEntity(new UrlEncodedFormEntiry(paimer,"UTF-8"));
HttpResponse response = client.excute(post);
int code = response.getStatusLine().getStatusCode();
if(code == 200){
InputStream is = response.getEntity().getContent();
String text = StreamTool.readStream(is);
return text;
}else{
return null;
}
}
以上是关于客服端与服务器端JSON数据交互的POST传输:的主要内容,如果未能解决你的问题,请参考以下文章
哪为IT大神帮个忙,Android客服端如何能够控制视图层监听的数据传输到服务端?
(4)优化TCP编写 客服端上传图片,服务端给客服端发消息问好