我需要抓住Android中SET NOCOUNT ON
DECLARE @RunCommand TABLE(RunCommand nvarchar(max))
DECLARE @SQL nvarchar(max),
@SQL2 nvarchar(max),
@FullSql nvarchar(max)
SET @SQL='
---create dummy tables for ''Employee'', and ''Attendence''
declare @tblEmployee table(
EmployeeID int,
EmployeeCode varchar(10),
EmployeeName varchar(255)
)
declare @tblAttendence table(
AttendenceID int identity(1, 1),
EmployeeID int,
AttendenceDate date,
AttendenceDay int,
Absence int
)
-----insert dummy records in both tables
declare @EmployeeID int = 1
declare @EmployeeCode varchar(10)
declare @EmployeeName varchar(255)
declare @CountEmployee int = 1
declare @CountDays int = 1
declare @UserYear int = 2017
declare @UserMonth int = 5
declare @DateFirst date = ''1 july 2017''
declare @DateLast date = ''31 july 2017''
declare @DateCurrent date = @DateFirst
while @CountEmployee <= 5
begin
set @EmployeeID = @CountEmployee
set @EmployeeCode = (''E00'' + CAST(@CountEmployee as varchar(10)))
set @EmployeeName = (''Emp_Name_'' + CAST(@CountEmployee as varchar(10)))
insert into @tblEmployee(
EmployeeCode ,
EmployeeID ,
EmployeeName
)
select
@EmployeeCode as EmployeeCode ,
@EmployeeID as EmployeeID ,
@EmployeeName as EmployeeName;
set @DateCurrent = @DateFirst
set @CountDays = 1
while @DateCurrent <= @DateLast
begin
insert into @tblAttendence(
EmployeeID ,
AttendenceDate ,
AttendenceDay,
Absence
)
select
@EmployeeID as EmployeeID ,
@DateCurrent as AttendenceDate ,
day(@DateCurrent) as AttendenceDay,
CAST(ROUND(RAND(),0) AS BIT) as Absence
set @DateCurrent = DATEADD(day, @CountDays, @DateFirst)
set @CountDays = @CountDays + 1
end
set @CountEmployee = @CountEmployee + 1
end
'
SET @SQL2='
;With cte
AS
(
SELECT ROW_NUMBER()OVER(Order by (SELECT 1)) AS Rno
FROM master..spt_values
)
,Ct2 AS
(
SELECT Rno FROM cte WHERE Rno <=31
)
SELECT DISTINCT CONCAT(''SELECT e.*,'',
STUFF((SELECT '', ''+'' t''+CAST(Rno AS VARCHAR(10))+''.Absence AS [Day''+CAST(Rno AS VARCHAR(10))+'']'' FROM Ct2 i
FOR XML PATH ('''')),1,1,''''),'' FROM @tblEmployee e'',
STUFF((SELECT '' ''+'' LEFT OUTER JOIN @tblAttendence t''+CAST(Rno AS VARCHAR(10))+'' ON t''+CAST(Rno AS VARCHAR(10))+''.EmployeeID = e.EmployeeID AND t''+CAST(Rno AS VARCHAR(10))+''.AttendenceDay =''+CAST(Rno AS VARCHAR(10)) FROM Ct2 i
FOR XML PATH ('''')),1,1,''''))
FROM Ct2 o
'
INSERT INTO @RunCommand
EXEC( @SQl2)
SELECT @SQL2=RunCommand FROM @RunCommand
SET @FullSql=@SQL+@SQL2
PRINT @FullSql
EXEC(@FullSql)
SET NOCOUNT OFF
请求的JSON
响应。
到目前为止,这是我的代码:
POST
如果ResponseCode为200(一切正常),服务器会向我发送一个包含我需要的数据的字符串。没问题,我发布的代码处理它没有问题。 200响应:
String data = null;
try {
data = URLEncoder.encode("Field1", "UTF-8")
+ "=" + URLEncoder.encode(field1, "UTF-8");
data += "&" + URLEncoder.encode("Field2", "UTF-8") + "="
+ URLEncoder.encode(field2, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String text = "";
BufferedReader reader=null;
// Send data
try
{
// Defined URL where to send data
URL url = new URL(Constants.URL_EMAIL_LOGIN);
// Send POST data request
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
int number = conn.getResponseCode();
// Get the server response
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
// Append server response in string
sb.append(line + "\n");
}
text = sb.toString();
}
catch(Exception ex)
{
}
finally
{
try
{
reader.close();
}
catch(Exception ex) {}
}
但我也需要抓住错误。在这种情况下,服务器返回"{\r\n \"user\": \"AAAA\",\r\n \"token\": \" \",\r\n \"email\": \"test@test.com\" \r\n}"
。这是错误的响应(我使用Firefox的Poster扩展):
JSON
有了这个回复,我的应用就崩溃了:
{"Message":"Field1 is not correct."}
这是捕获的错误:
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
有谁知道如何从服务器读取java.io.FileNotFoundException: [url]
?
答案 0 :(得分:1)
错误是由conn.getInputStream()
方法引起的。如果服务器返回的响应代码大于或等于HttpUrlConnection
FileNotFoundException
会在getInputStream()
上抛出400
例外
当响应状态不是conn.getErrorStream()
时,使用200
获取输入流
检查一下:
BufferedInputStream inputStream = null;
int status = conn.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
inputStream = conn.getErrorStream();
} else {
inputStream = conn.getInputStream();
}
并在reader
中使用它:
reader = new BufferedReader(new InputStreamReader(inputStream));