80,351
社区成员
发帖
与我相关
我的任务
分享
public int doPost(String stringUrl, JSONObject json) {
String result = null;
HttpPost post = new HttpPost(stringUrl);
HttpResponse httpResponse = null;
try {
StringEntity entity=new StringEntity(json.toString(),HTTP.UTF_8);
entity.setContentType("application/json");
post.setEntity(entity);
httpResponse = new DefaultHttpClient().execute(post);
return httpResponse.getStatusLine().getStatusCode() ;
}
catch (Exception e) {
e.printStackTrace();
return 0;
}
}
//解析json
// 如果json不为空 则解析
//返回json
JSONObject jsonBack=new JSONObject();
jsonBack.put("currentUpdateDate", "currentUpdateDate");
byte[] jsonBytes;
try {
jsonBytes = jsonBack.toString().getBytes("utf-8");
response.setContentType("text/html;charset=utf-8");
response.setContentLength(jsonBytes.length);
response.getOutputStream().write(jsonBytes);
response.getOutputStream().flush();
response.getOutputStream().close();
// response.getWriter().write(jsonBack.toString());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
以上是服务端做出相应的代码,上面的接收并解析我没写,这就是我纠结一早上的问题
//得到反馈结果
if(n==0){
//连接失败
Toast.makeText(getApplicationContext(), "连接失败",Toast.LENGTH_LONG ).show();
}else{
JSONObject jsonObject;
try {
jsonObject = new JSONObject();
//解析JSON
String login = jsonObject.getString("currentUpdateDate");
Toast.makeText(getApplicationContext(), "success"+login,Toast.LENGTH_LONG ).show();
} catch (JSONException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}