从android调用时,在java Rest客户端获取参数的空值

时间:2016-03-09 11:05:25

标签: java android web-services rest

从android调用时获取参数的空值..

  

网址:http://[serverip]:8080/SalesTracker/Sales/salesService/insertInfo?imei=11112345&date_of_activation=2016-02-18&manufacturer=1&modelname=zx2&locationgps=dhaka&locationgprs=dhaka&locationgooglemap=dhaka

获取以下错误:

INSERT INTO salse_track (IMEI, DATE_OF_ACTIVATION, MANUFACTURER,MODEL_NAME,LOCATION_GPS,LOCATION_GPRS,LOCATION_GOOGLEMAP) VALUES (?, ?, ?,?,?,?,?)
com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Column 'IMEI' cannot be null

这是我的代码:

package services;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;

import model.AccessManager;

@Path("/salesService")
public class SalesServices {
    @PUT
    @Path("/insertInfo")
    @Produces(MediaType.APPLICATION_JSON)


    public String users(@QueryParam("imei") String imei, @QueryParam("activationdate") String activationdate, 
            @QueryParam("manufacturer") String manufacturer, @QueryParam("modelname") String modelname, @QueryParam("locationgps") String locationgps, 
            @QueryParam("locationgprs") String locationgprs, @QueryParam("locationgooglemap") String locationgooglemap) throws IOException {

        int result = 0;
        String output = "Prameter1: " + imei + "\nParameter2: " + activationdate;
        System.out.println(output);
            result = new AccessManager().addInfo(imei, activationdate, manufacturer, modelname, locationgps,
                    locationgprs, locationgooglemap);


        if (result == 1) {
            return "success";
            // http://192.168.25.254:8080/SalesTracker/Sales/salesService/insertInfo?imei=11112345&date_of_activation=2016-02-18&manufacturer=1&modelname=zx2&locationgps=dhaka&locationgprs=dhaka&locationgooglemap=dhaka
        } else
            return "unsuccessful";
    }
}

我的Android代码如下:

public void dss()
{
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
    .permitAll().build();

StrictMode.setThreadPolicy(policy);
     URL url;
        HttpURLConnection connection = null;  
        try {
          //Create connection
          url = new URL("http://192.168.134.175:8080/SalesTracker/Sales/salesService/insertInfo?");
          connection = (HttpURLConnection)url.openConnection();
          connection.setRequestMethod("PUT");
          connection.setRequestProperty("Content-Type","application/json");
          String urlParameters1 = "imei=1111224345&date_of_activation=2016-02-18&manufacturer=1&modelname=zx2&locationgps=dhaka&locationgprs=dhaka&locationgooglemap=dhaka";
          String urlParameters = URLEncoder.encode(urlParameters1, "UTF-8");
          connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
          connection.setRequestProperty("Content-Language", "UTF-8");  

          connection.setUseCaches (false);
          connection.setDoInput(true);
          connection.setDoOutput(true);

          //Send request
          DataOutputStream wr = new DataOutputStream (
                      connection.getOutputStream ());
          wr.writeBytes (urlParameters);
          wr.flush ();
          wr.close ();

          //Get Response    
          InputStream is = connection.getInputStream();
          BufferedReader rd = new BufferedReader(new InputStreamReader(is));
          String line;
          StringBuffer response = new StringBuffer(); 
          while((line = rd.readLine()) != null) {
            response.append(line);
            response.append('\r');
          }
          rd.close();
         String getFinalResponse=response.toString();
         Toast.makeText(_mContext, ""+getFinalResponse.toString(), Toast.LENGTH_LONG).show();

        } catch (Exception e) {

          e.printStackTrace();
     Toast.makeText(_mContext, ""+e.toString(), Toast.LENGTH_LONG).show();

        } finally {

          if(connection != null) {
            connection.disconnect(); 
          }
        }
}

1 个答案:

答案 0 :(得分:0)

您已使用@PathParam对方法参数进行了注释,但它们是@QueryParam&#39}。您的路径为:/SalesTracker/Sales/salesService/insertInfo,不包含参数imei。但您的查询参数确实如此。

尝试使用@QueryParam

注释你的参数