If you think the Android project socialcde4android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package it.uniba.socialcde4android.data.operation;
/*fromwww.java2s.com*/import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import android.content.Context;
import android.os.Bundle;
import it.uniba.socialcde4android.config.Config;
import it.uniba.socialcde4android.costants.Consts;
import it.uniba.socialcde4android.costants.Error_consts;
import it.uniba.socialcde4android.preferences.Preferences;
import it.uniba.socialcde4android.shared.library.WUser;
import com.foxykeep.datadroid.exception.ConnectionException;
import com.foxykeep.datadroid.exception.CustomRequestException;
import com.foxykeep.datadroid.exception.DataException;
import com.foxykeep.datadroid.requestmanager.Request;
import com.foxykeep.datadroid.service.RequestService.Operation;
//import com.google.gson.Gson;
import com.google.gson.Gson;
publicclass GetUser_Operation implements Operation {
//private static final String TAG = GetUser_Operation.class.getSimpleName();
@Override
public Bundle execute(Context context, Request request)
throws ConnectionException, DataException, CustomRequestException {
String userName = request.getString(Preferences.USERNAME);
String password = request.getString(Preferences.PASSWORD);
String host = request.getString(Preferences.PROXYSERVER) + "/SocialTFSProxy.svc";
int status =0;
WUser wuser = new WUser();
try {
URL url = new URL(host + "/GetUser");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(Config.CONN_TIMEOUT_MS);
conn.setReadTimeout(Config.READ_TIMEOUT_MS);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setAllowUserInteraction(false);
conn.setRequestProperty("Content-Type", "application/json");
// Create the form content
OutputStream out = conn.getOutputStream();
Writer writer = new OutputStreamWriter(out, "UTF-8");
writer.write("{ \"username\":\"" + userName + "\", \"password\":\""
+ password + "\"}");
writer.close();
out.close();
status = conn.getResponseCode();
if (status >= 200 && status <= 299) {
InputStreamReader in = new InputStreamReader(
conn.getInputStream());
BufferedReader br = new BufferedReader(in);
String output;
String result = "";
while ((output = br.readLine()) != null) {
result += output;
}
br.close();
Gson gson = new Gson();
wuser = gson.fromJson(result, WUser.class);
}else{
thrownew ConnectionException("Error getting the user",Error_consts.ERROR_GETTING_USER);
}
conn.disconnect();
}catch(java.net.SocketTimeoutException e) {
thrownew ConnectionException("Error getting the user",Error_consts.ERROR_GETTING_USER * Error_consts.TIMEOUT_FACTOR);
} catch (Exception e) {
thrownew ConnectionException("Error getting the user",Error_consts.ERROR_GETTING_USER);
}
Bundle bundle = new Bundle();
if (wuser != null){
bundle.putParcelable(Consts.WUSER, wuser);
bundle.putBoolean(Consts.FOUND_WUSER, true);
}else{
bundle.putBoolean(Consts.FOUND_WUSER, false);
}
bundle.putInt(Consts.REQUEST_TYPE, Consts.REQUESTTYPE_GETUSER);
return bundle;
}
}