点子:
来自android
Sendind到服务器: - 用户名和国家/地区名称 从服务器接收: - 用户的一些细节。
所以,我有 服务器发送到android
List<User> findUser=userDao.findUser(sff.getFullname(), sff.getCountry());
for (User usser : findUser) {
logger.info(usser.getCountry());
}
return new ResponseEntity<Object>(findUser, HttpStatus.OK);
客户端代码连接&amp;从服务器接收数据
@Override
public String userSearch(String fullname, String country) throws Exception {
// initialise URL for mobile user sign up POST
String url1 = PE_URL + PE_SEARCHBYNAME;
// Instantiate and initialise a SignupForm
SearchUser su = new SearchUser(fullname, country);
URL url = new URL(url1);
String urlParameters = "suf="+ URLEncoder.encode(new Gson().toJson(su), "UTF-8");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setFixedLengthStreamingMode(urlParameters.getBytes().length);
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
conn.setRequestProperty("Application-Id",
"Ajrehrwweg22");
conn.setDoInput(true);
try {
PrintWriter out = new PrintWriter(conn.getOutputStream());
out.print(urlParameters);
out.close();
// build the string to store the response text from the server
sb = new StringBuilder();
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
}
Log.d("MyApp", TAG + " > server response result: " + sb.toString());
return sb.toString();
} catch (IOException ioe) {
in = new BufferedReader(
new InputStreamReader(conn.getErrorStream()));
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
}
throw new Exception(sb.toString());
}
}
MainActivity背景代码
private void serverSearchUser(final SearchUser su) {
AsyncTask<SearchUser, Void, String> execute =
new AsyncTask<SearchUser, Void, String>() {
@Override
protected String doInBackground(SearchUser... params) {
String serverResponse = "";
try {
serverResponse = mServerAuth.userSearch(su.getFullname(), suff.getCountry());
} catch (Exception e) {
}
return serverResponse;
}
@Override
protected void onPostExecute(String serverResponse) {
// do we have the authtoken?
if (serverResponse instanceof String) {
SearchUser suf = new Gson().fromJson(serverResponse, SearchUser.class);
} else {
// we have an error in the data sent
Error error;
try {
error = new Gson().fromJson(serverResponse, Error.class);
EditText errorField = (EditText) getActivity().findViewById(R.id.name)
.findViewWithTag(error.getField());
if (errorField != null) {
try {
errorField.setError(URLDecoder.decode(error.getMessage(), "UTF-8"));
} catch (UnsupportedEncodingException uee) {
}}}catch (JsonSyntaxException jse) {
}}}
}.execute();
}
从服务器成功发送数据到android但在android响应中 我收到了错误:
access server error: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2
答案 0 :(得分:1)
当您尝试从响应中获取单个对象时,GSON可能会返回一个USER类型数组。在logcat上打印响应.. 试着改变
SearchUser suf = new Gson().fromJson(serverResponse, SearchUser.class);
到
SearchUser[] suf = new Gson().fromJson(serverResponse, SearchUser[].class);