从android应用程序向java web服务方法传递参数
我想从android应用程序执行java web服务方法。我的web服务方法签名如下所示:从android应用程序向java web服务方法传递参数,java,android,web-services,Java,Android,Web Services,我想从android应用程序执行java web服务方法。我的web服务方法签名如下所示: public String getData(String category) throws Exception 该方法返回字符串并接受字符串作为参数 我在google chrome的地址栏中执行了以下方法: http://localhost:8080/JsonWebService/services/JsonWebService/getData?category=Marketing 这里getData是
public String getData(String category) throws Exception
该方法返回字符串并接受字符串作为参数
我在google chrome的地址栏中执行了以下方法:
http://localhost:8080/JsonWebService/services/JsonWebService/getData?category=Marketing
这里getData是方法的名称,Marketing是该方法的参数。在explorer中,它工作正常
但当我向android应用程序的httppost请求添加相同的url时,它无法说出错误的参数数。我的android应用程序代码是:
HttpPost post = new HttpPost("http://192.168.1.7:8080/JsonWebService/services/JsonWebService/getData?category=Marketing");
HttpResponse httpres = httpClient.execute(post);
请注意:这里需要192.168.1.7,因为我直接在设备上执行应用程序,因此我不使用localhost
一个无参数方法也可以从android应用程序正确执行。
但当参数插入到url中时,为什么在android应用程序中无法说出错误的参数数,以及它如何在PC上的google chrome中正确执行。请帮助。。。谢谢
我在这里添加代码。我的java web服务代码如下:
public class JsonWebService {
@POST
@Path("getData")
public String getData(String category) throws Exception {
JSONObject jsonData = new JSONObject();
String Email = "";
String Name = "";
String receivedCat = "";
boolean status = false;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/admindb","root","");
java.sql.PreparedStatement query = con.prepareStatement("SELECT * FROM sample WHERE Category =" + "'" + category + "'" + ";");
ResultSet result = query.executeQuery();
while(result.next()){
receivedCat = result.getString("Category");
Name = result.getString("Name");
Email = result.getString("Email");
}
if(receivedCat.equals(category)){
status = true;
jsonData.put("Name",Name);
jsonData.put("Email", Email);
jsonData.put("status", status);
}
}
catch(Exception e) {
e.printStackTrace();
}
return jsonData.toString();
}
btnCategory = (Button)findViewById(R.id.button1);
txtCategory = (EditText)findViewById(R.id.editText1);
gridV = (GridView)findViewById(R.id.gridView1);
txtName = (EditText)findViewById(R.id.editText3);
txtEmail = (EditText)findViewById(R.id.editText2);
btnCategory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Thread netThread = new Thread() {
public void run() {
try {
final JSONObject receivedJson;// = new JSONObject();
String URL = "http://192.168.1.7:8080/JsonWebService/services/JsonWebService/getData?category=Marketing?";
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(URL);
HttpResponse httpres = httpClient.execute(post);
HttpEntity entity = httpres.getEntity();
String json = EntityUtils.toString(entity).toString();
String parts[] = json.split("<ns:return>");
parts = parts[1].split("</ns:return>");
String jsonPart = parts[0];
receivedJson = new JSONObject(jsonPart);
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
txtName.setText(receivedJson.getString("Name"));
txtEmail.setText(receivedJson.getString("Email"));
}
catch(Exception e){
}
}
};
netThread.start();
}
});
}
我的android客户端代码如下所示:
public class JsonWebService {
@POST
@Path("getData")
public String getData(String category) throws Exception {
JSONObject jsonData = new JSONObject();
String Email = "";
String Name = "";
String receivedCat = "";
boolean status = false;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/admindb","root","");
java.sql.PreparedStatement query = con.prepareStatement("SELECT * FROM sample WHERE Category =" + "'" + category + "'" + ";");
ResultSet result = query.executeQuery();
while(result.next()){
receivedCat = result.getString("Category");
Name = result.getString("Name");
Email = result.getString("Email");
}
if(receivedCat.equals(category)){
status = true;
jsonData.put("Name",Name);
jsonData.put("Email", Email);
jsonData.put("status", status);
}
}
catch(Exception e) {
e.printStackTrace();
}
return jsonData.toString();
}
btnCategory = (Button)findViewById(R.id.button1);
txtCategory = (EditText)findViewById(R.id.editText1);
gridV = (GridView)findViewById(R.id.gridView1);
txtName = (EditText)findViewById(R.id.editText3);
txtEmail = (EditText)findViewById(R.id.editText2);
btnCategory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Thread netThread = new Thread() {
public void run() {
try {
final JSONObject receivedJson;// = new JSONObject();
String URL = "http://192.168.1.7:8080/JsonWebService/services/JsonWebService/getData?category=Marketing?";
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(URL);
HttpResponse httpres = httpClient.execute(post);
HttpEntity entity = httpres.getEntity();
String json = EntityUtils.toString(entity).toString();
String parts[] = json.split("<ns:return>");
parts = parts[1].split("</ns:return>");
String jsonPart = parts[0];
receivedJson = new JSONObject(jsonPart);
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
txtName.setText(receivedJson.getString("Name"));
txtEmail.setText(receivedJson.getString("Email"));
}
catch(Exception e){
}
}
};
netThread.start();
}
});
}
btncategray=(按钮)findViewById(R.id.button1);
txtCategory=(EditText)findViewById(R.id.editText1);
gridV=(GridView)findviewbyd(R.id.gridView1);
txtName=(EditText)findViewById(R.id.editText3);
txtEmail=(EditText)findViewById(R.id.editText2);
btncategray.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
线程netThread=新线程(){
公开募捐{
试一试{
最终JSONObject receivedJson;/=新JSONObject();
字符串URL=”http://192.168.1.7:8080/JsonWebService/services/JsonWebService/getData?category=Marketing?";
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost=新的HttpPost(URL);
HttpResponse httpres=httpClient.execute(post);
HttpEntity entity=httpres.getEntity();
字符串json=EntityUtils.toString(entity.toString();
字符串部分[]=json.split(“”);
零件=零件[1]。拆分(“”);
字符串jsonPart=parts[0];
receivedJson=新的JSONObject(jsonPart);
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
试一试{
setText(receivedJson.getString(“名称”));
setText(receivedJson.getString(“电子邮件”));
}
捕获(例外e){
}
}
};
netThread.start();
}
});
}
问题出在java客户端的字符串URL中,这是一个调用java web服务方法的URL字符串。请帮助我…这是一个get请求,不要使用post。在浏览器中浏览URL是一个get请求。我没有使用“new HttpPost”而是使用“new HttpGet”,但存在相同的问题。它是从chrome执行的,而不是从android应用程序执行的。Ple请提供帮助…请让我看看您执行的代码我已经修改了问题并插入了完整的代码。请解释为什么我使用的URL不起作用。我甚至在客户端使用了HttpGet并用@GET修改了web服务,但问题是相同的。