简体   繁体   English

使用HttpPost从Android客户端应用程序将参数传递给Java Web Service方法时出错

[英]Error passing arguments to java web service method from android client app using HttpPost

I want to pass arguments to java web service from android client app. 我想将参数从android客户端应用程序传递给java网络服务。 But I am getting error. 但是我遇到了错误。 My java web service code is as follows: 我的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();
}

My android client code looks as follows: 我的android客户端代码如下所示:

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";
                    StringEntity params = new StringEntity("{\"category\":\"Marketing\"}");
                    DefaultHttpClient httpClient = new DefaultHttpClient();
                    HttpPost post = new HttpPost(URL);
                    post.setEntity(params);

                    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();
            }
        });
}

I am getting following error: 我收到以下错误:

Caused by: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '{' (code 123) in prolog; expected '<' at [row,col {unknown-source}]: [1,1]
at com.ctc.wstx.sr.StreamScanner.throwUnexpectedChar(StreamScanner.java:648)
at com.ctc.wstx.sr.BasicStreamReader.nextFromProlog(BasicStreamReader.java:2047)
at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1069)
at org.apache.axiom.util.stax.wrapper.XMLStreamReaderWrapper.next(XMLStreamReaderWrapper.java:225)
at org.apache.axiom.om.impl.builder.StAXOMBuilder.parserNext(StAXOMBuilder.java:668)
at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:214)
   ... 26 more
[INFO] Remaining input stream :[]

Please help me to solve this problem...Thanks... 请帮我解决这个问题...谢谢...

Your webservice method is not expecting json as input Media Type. 您的网络服务方法不希望将json作为输入媒体类型。

Make use of JAX-RS speficication's @Consumes and @Produces annotations to tell your service what to expect and what to deliver: 利用JAX-RS的@Consumes和@Produces批注来告诉您的服务期望什么和交付什么:

@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
@Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")

You might also need to include the jersey-json libraries to your project. 您可能还需要将jersey-json库包含到您的项目中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM