我试图从servlet获取数据但失败了。我使用HttpUrlConnection类而不是HttpClient或HttpConnection而且' get'方法。我找不到有什么问题。 Android应用程序没有失败,但他们无法从服务器获取任何json字符串。 android日志,servlet代码和android代码就在这里。
和我真的想知道什么是SBSetting,ShipBuild和SmartBonding并解决了这个问题。 请帮帮我!
12-31 15:01:16.279 26003-26625/capston.stol.dangerousplace I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
12-31 15:01:16.279 26003-26625/capston.stol.dangerousplace I/System.out: (HTTPLog)-Static: isShipBuild true
12-31 15:01:16.279 26003-26625/capston.stol.dangerousplace I/System.out: (HTTPLog)-Thread-3535-332636781: SmartBonding Enabling is false, SHIP_BUILD is true, log to file is false, DBG is false
12-31 15:01:16.279 26003-26625/capston.stol.dangerousplace I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
12-31 15:01:16.289 26003-26625/capston.stol.dangerousplace I/System.out: KnoxVpnUidStorageknoxVpnSupported API value returned is false
这是Servlet代码。
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
/**
* Servlet implementation class WarningInfoView
*/
@WebServlet("/warning/view")
public class WarningInfoView extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
double xGps = 37.869972;
double yGps = 127.743389;
try {
ServletContext sc = this.getServletContext();
Class.forName(sc.getInitParameter("driver"));
conn = DriverManager.getConnection(
sc.getInitParameter("url"),
sc.getInitParameter("username"),
sc.getInitParameter("password"));
stmt = conn.createStatement();
xGps = Double.parseDouble(request.getParameter("xcoord"));
yGps = Double.parseDouble(request.getParameter("ycoord"));
rs = stmt.executeQuery("SELECT idx,w_count,xcoord,ycoord,title FROM warning_info"
+ " WHERE (xcoord BETWEEN "
+(xGps-0.00447625)+" AND "+(xGps+0.00447625)+ ") AND (" //위도 +-500m
+"ycoord BETWEEN "
+(yGps-0.055329)+" AND "+(yGps+0.055329)+");"); //경도 +-500m
JSONObject jsonMain = new JSONObject();
JSONArray jArray = new JSONArray();
while(rs.next()){
JSONObject jObject = new JSONObject();
int idx = rs.getInt("idx");
int w_count = rs.getInt("w_count");
double xcoord = rs.getDouble("xcoord");
double ycoord = rs.getDouble("ycoord");
String title = rs.getString("title");
jObject.put("idx", idx);
jObject.put("w_count",w_count);
jObject.put("xcoord",xcoord);
jObject.put("ycoord",ycoord);
jObject.put("title",title);
jArray.add(0,jObject);
}
jsonMain.put("List", jArray);
System.out.println(jsonMain.toJSONString());
String json = jsonMain.toJSONString();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
}catch (Exception e) {
throw new ServletException(e);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
而且,这是Android代码。
private class SndMyLocReqWarnInfoList extends AsyncTask<String, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(String... param){
String lat = param[0];
String lng = param[1];
try {
Log.e("latlng", lat + ", " + lng);
Properties prop = new Properties();
prop.setProperty("xcoord", lat);
prop.setProperty("ycoord", lng);
String encodedString = encodeString(prop);
URL url = new URL(getString(R.string.MainURL) + getString(R.string.SndMyLocReqWarnInfoList) + encodedString);
Log.e("URL", url + "");
HttpURLConnection conn= (HttpURLConnection)url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setRequestProperty("Accept", "application/json");
conn.setRequestMethod("GET");
try {
int responseCode = conn.getResponseCode();
Log.e("respCode", responseCode+"");
if(responseCode == HttpURLConnection.HTTP_OK){
InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = "";
String res = "";
while((line == br.readLine()) == true){
res += line;
}
Log.e("result json:", res);
is.close();
}
} catch (Exception e){
Log.e("getwilist ", "error");
e.printStackTrace();
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onCancelled() {
super.onCancelled();
}
}
public static String encodeString(Properties params) {
StringBuffer sb = new StringBuffer(256);
Enumeration names = params.propertyNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
String value = params.getProperty(name);
sb.append(URLEncoder.encode(name) + "=" + URLEncoder.encode(value) );
if (names.hasMoreElements()) sb.append("&");
}
return sb.toString();
}
答案 0 :(得分:0)
使用Post Man Chrome检查您是否收到有效回复。 我正在使用以下使用httpuurlconnection。请在下方使用并查看。
public String sendGet(String apiUrl) {
StringBuilder result = new StringBuilder();
HttpURLConnection urlConnection = null;
try {
URL url = new URL(apiUrl);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
urlConnection.disconnect();
}
return result.toString();
}