Reputation: 747
Idea:
From android
Sendind to server:--username and country name receive from server :-- some details of the user.
So, I have server send to 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);
Client code connecting & receiving data from server
@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 background code
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();
}
From server successfully sends data to android But In android response I am getting error:
access server error: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2
Upvotes: 0
Views: 156
Reputation: 23881
Maybe GSON returns you an array of USER type while you are trying to get a single object from the response. Print the response on logcat.. try to change
SearchUser suf = new Gson().fromJson(serverResponse, SearchUser.class);
to
SearchUser[] suf = new Gson().fromJson(serverResponse, SearchUser[].class);
Upvotes: 1