override func viewDidLoad() {
super.viewDidLoad()
let label = UILabel()
let myMutableString = NSMutableAttributedString()
//right-to-left
let multipleAttributes: [String : AnyObject] = [
NSForegroundColorAttributeName: UIColor.orangeColor(),
NSBackgroundColorAttributeName: UIColor.blueColor(),
NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleDouble.rawValue,
NSWritingDirectionAttributeName : [NSWritingDirection.LeftToRight.rawValue ]
]
let myAttrString = NSAttributedString(string: "عبد الله", attributes: multipleAttributes)
myMutableString.appendAttributedString(myAttrString)
//some-text
let someText = NSAttributedString(string: " finds ", attributes: nil)
myMutableString.appendAttributedString(someText)
//left-to-right
let multipleAttributes2: [String : AnyObject] = [
NSForegroundColorAttributeName: UIColor.blueColor(),
NSBackgroundColorAttributeName: UIColor.yellowColor(),
NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleDouble.rawValue,
NSWritingDirectionAttributeName : [NSWritingDirection.LeftToRight.rawValue | NSTextWritingDirection.Embedding.rawValue]
]
let myAttrString2 = NSAttributedString(string: "restaurant", attributes: multipleAttributes2)
myMutableString.appendAttributedString(myAttrString2)
label.attributedText = myMutableString
self.view.addSubview(label)
label.sizeToFit()
label.center = self.view.center
}
这是客户端代码......
public class DSprojectClient {
public static void main(String[] args) {
Client client = Client.create();
Gson gson = new Gson();
String method = "N2";
WebResource webResource = client.resource("http://localhost:8080/EmployeeManage/demo/DS/" + method);
EmployeeAddRequest addRequest = new EmployeeAddRequest();
addRequest.setId(1103);
String json = gson.toJson(addRequest/* , EmployeeAddRequest.class */);
System.out.println(json);
// String id = "1103";
// String input = "{\"id\":\"1103\"}";
ClientResponse response = webResource.type("application/json").post(ClientResponse.class, json);
System.out.println("response" + response);
System.out.println("Output from Server .... \n");
String output = response.toString();
System.out.println("output\n" + output);
EmployeeResponse addResponse = new EmployeeResponse();
addResponse = gson.fromJson(output, EmployeeResponse.class);
System.out.println(output);
System.out.println(addResponse.getId() + ****************************");
System.out.println(output);
}
}
这是json代码
@Path("/DS")
public class Standardization {
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/N2")
public EmployeeResponse getDetails1(EmployeeRequest employeeRequest) throws Exception{
DsUserService dsUserService = new DsUserService();
System.out.println("getDetails1::object created");
EmployeeResponse retval = dsUserService.AddEmp(employeeRequest.getId());
return retval;
}
}
点击db ...
输出
public class DsUserService {
Gson gson = new Gson();
PreparedStatement stmt = null;
static Connection conn = null;
static {
try {
Class.forName("com.mysql.jdbc.Driver");
DB_Connector.getInstance();
System.out.println(" Connection made :: "+DB_Connector.connection);
conn = DB_Connector.connection; // Get DB Connection
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public EmployeeResponse AddEmp(int id) throws Exception{
EmployeeResponse retval = new EmployeeResponse();
if ( conn != null ) {
System.out.println(" DB Connection Successful ");
String query = "Select name FROM harish_table_employee WHERE id="+id;
stmt = conn.prepareStatement(query);
ResultSet rs = stmt.executeQuery();
if (rs.next()){
retval.setName(rs.getString("name"));
retval.setId(rs.getInt("id"));
}
}
return retval;
}
}
请帮帮我..出了什么问题?
答案 0 :(得分:0)
似乎您需要直接处理对象,而不需要在JSON中进行转换。您可以更改post参数:
ClientResponse response = webResource.type("application/json")
.post(ClientResponse.class, json);
直接发送对象,而不是json,如下:
ClientResponse response = webResource.type("application/json")
.post(ClientResponse.class, addRequest);
或者,在服务器端,您可以添加一个将String作为输入参数的服务:
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/N2")
public EmployeeResponse getDetails2(String employeeRequestJson) throws Exception{
DsUserService dsUserService = new DsUserService();
System.out.println("getDetails1::object created");
EmployeeRequest employeeRequest = gson.fromJson(employeeRequestJson, EmployeeRequest.class);
EmployeeResponse retval = dsUserService.AddEmp(employeeRequest.getId());
return retval;
}