如题,我在android中通过一下代码与Struts2的Action类通信,不懂怎么把Json数据传到服务器端的Action?经过debug发现到了(A)、(B)处都不能连接到服务器端,而(C)处则能够成功连接到服务器端的Action类,并且Action类有反应,服务器端的Action类在运行完后也能返回到android程序端继续运行,但是json数据为什么就是无法传递?输入输出流还是无法取得数据,请哪个朋友帮忙看下,谢谢。android程序端的代码:
public static boolean getURLConn(String urlString,String[] keys,String[] values){
boolean isTrue=false;
URL url;
HttpURLConnection conn=null;
try {
url = new URL(urlString);
conn=(HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
// 设置以POST方式
conn.setRequestMethod("POST");
// Post 请求不能使用缓存
conn.setUseCaches(false);
conn.setInstanceFollowRedirects(true);
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.connect(); -------------------------(A)
String jsonStri=getJsonString(keys,values); 此处是把数据转化成json数据类型的字符串
OutputStream os=conn.getOutputStream(); -------------------------(B)
DataOutputStream dos=new DataOutputStream(os);
dos.writeBytes("jsonStri="+jsonStri);
dos.flush();
dos.close();
InputStream is=conn.getInputStream();--------------------------(C)
InputStreamReader reader=new InputStreamReader(is);
String jsonStr="";
int r=0;
while((r=reader.read())!=-1){
jsonStr+=(char)r;
}
System.out.println("---------------------"+jsonStr);
JSONObject json=new JSONObject(jsonStr);
isTrue=json.getBoolean("action");
is.close();
reader.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
conn.disconnect();
return isTrue;
}
服务器端的Action类代码:public String login() {
try {
InputStream is=request.getInputStream();
InputStreamReader reader=new InputStreamReader(is);
BufferedReader br=new BufferedReader(reader);
String jsonStr="";
int r=0;
while((r=reader.read())!=-1){
System.out.println(r);
jsonStr+=(char)r;
}
JSONObject json=new JSONObject(jsonStr);
String name=json.getString("name").trim();
String pass=json.getString("pass").trim();
if (name != null && pass != null) {
String[] values = { name, pass };
String sql = "select *from student where name=? and password=?";
JSONObject json1=new JSONObject(jsonStr);
if(query(sql,values)){ 此处是查询数据库,返回boolean类型数据
json1.put("action", true);
}
else
json1.put("action", false);
response.reset();
response.resetBuffer();
response.getOutputStream().write(json1.toString().getBytes());
response.getOutputStream().flush();
}
is.close();
reader.close();
br.close();
response.getOutputStream().close();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return Action.SUCCESS;
}
public static boolean getURLConn(String urlString,String[] keys,String[] values){
boolean isTrue=false;
URL url;
HttpURLConnection conn=null;
try {
url = new URL(urlString);
conn=(HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
// 设置以POST方式
conn.setRequestMethod("POST");
// Post 请求不能使用缓存
conn.setUseCaches(false);
conn.setInstanceFollowRedirects(true);
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.connect(); -------------------------(A)
String jsonStri=getJsonString(keys,values); 此处是把数据转化成json数据类型的字符串
OutputStream os=conn.getOutputStream(); -------------------------(B)
DataOutputStream dos=new DataOutputStream(os);
dos.writeBytes("jsonStri="+jsonStri);
dos.flush();
dos.close();
InputStream is=conn.getInputStream();--------------------------(C)
InputStreamReader reader=new InputStreamReader(is);
String jsonStr="";
int r=0;
while((r=reader.read())!=-1){
jsonStr+=(char)r;
}
System.out.println("---------------------"+jsonStr);
JSONObject json=new JSONObject(jsonStr);
isTrue=json.getBoolean("action");
is.close();
reader.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
conn.disconnect();
return isTrue;
}
服务器端的Action类代码:public String login() {
try {
InputStream is=request.getInputStream();
InputStreamReader reader=new InputStreamReader(is);
BufferedReader br=new BufferedReader(reader);
String jsonStr="";
int r=0;
while((r=reader.read())!=-1){
System.out.println(r);
jsonStr+=(char)r;
}
JSONObject json=new JSONObject(jsonStr);
String name=json.getString("name").trim();
String pass=json.getString("pass").trim();
if (name != null && pass != null) {
String[] values = { name, pass };
String sql = "select *from student where name=? and password=?";
JSONObject json1=new JSONObject(jsonStr);
if(query(sql,values)){ 此处是查询数据库,返回boolean类型数据
json1.put("action", true);
}
else
json1.put("action", false);
response.reset();
response.resetBuffer();
response.getOutputStream().write(json1.toString().getBytes());
response.getOutputStream().flush();
}
is.close();
reader.close();
br.close();
response.getOutputStream().close();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return Action.SUCCESS;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货