java - Return string from try catch block -


i'm trying return string foo function, application crashes.

public string foo (string email){     string url_set = "http://****.php?email=" + email;     string responsestr = "1";     try {         httpclient httpclient = new defaulthttpclient();         httppost httppost = new httppost(url_set);         httpresponse response = httpclient.execute(httppost);         return responsestr;     } catch (unsupportedencodingexception e) {         e.printstacktrace();         return responsestr;     } catch (clientprotocolexception e) {         log.e("clientprotocolexception", "client");         e.printstacktrace();         return responsestr;     } catch (ioexception e) {         log.e("ioexception", "ioexption");         e.printstacktrace();         return responsestr;     } } 

in general want convert response string , return caller. can't ever return string.

upd: logcat

04-28 14:13:57.220    2917-2917/com.example.lemon.onlineshop e/androidruntime﹕ fatal exception: main     process: com.example.lemon.onlineshop, pid: 2917     android.os.networkonmainthreadexception             @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1147)             @ java.net.inetaddress.lookuphostbyname(inetaddress.java:418)             @ java.net.inetaddress.getallbynameimpl(inetaddress.java:252)             @ java.net.inetaddress.getallbyname(inetaddress.java:215)             @ org.apache.http.impl.conn.defaultclientconnectionoperator.openconnection(defaultclientconnectionoperator.java:137)             @ org.apache.http.impl.conn.abstractpoolentry.open(abstractpoolentry.java:164)             @ org.apache.http.impl.conn.abstractpooledconnadapter.open(abstractpooledconnadapter.java:119)             @ org.apache.http.impl.client.defaultrequestdirector.execute(defaultrequestdirector.java:360)             @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:555)             @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:487)             @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:465)             @ com.example.lemon.onlineshop.library.sessionmanager.getstring(sessionmanager.java:88)             @ com.example.lemon.onlineshop.library.sessionmanager.createloginsession(sessionmanager.java:76)             @ com.example.lemon.onlineshop.mainactivity.executelogin(mainactivity.java:96)             @ com.example.lemon.onlineshop.mainactivity$login.onpostexecute(mainactivity.java:191)             @ com.example.lemon.onlineshop.mainactivity$login.onpostexecute(mainactivity.java:160)             @ android.os.asynctask.finish(asynctask.java:632)             @ android.os.asynctask.access$600(asynctask.java:177)             @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:645)             @ android.os.handler.dispatchmessage(handler.java:102)             @ android.os.looper.loop(looper.java:135)             @ android.app.activitythread.main(activitythread.java:5221)             @ java.lang.reflect.method.invoke(native method)             @ java.lang.reflect.method.invoke(method.java:372)             @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899)             @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694) 

upd2: added whole class

public class sessionmanager { // shared preferences sharedpreferences pref;  // editor shared preferences sharedpreferences.editor editor;  // context context _context;  // shared pref mode int private_mode = 0;  // sharedpref file name private static final string pref_name = "sessionpref";  // shared preferences keys private static final string is_login = "isloggedin";  // user name (make variable public access outside) public static final string key_email = "email";  // email address (make variable public access outside) public static final string key_password = "password";  // public static final string key_id = "id";  // constructor public sessionmanager(context context){     this._context = context;     pref = _context.getsharedpreferences(pref_name, private_mode);     editor = pref.edit(); }  /**  * create login session  * */ public void createloginsession(string email, string password){     // storing login value true     editor.putboolean(is_login, true);      // storing name in pref     editor.putstring(key_email, email);      // storing email in pref     editor.putstring(key_password, password);      // storing id in pref     editor.putstring(key_id, getid(email));     // commit changes     editor.commit(); }  public string getid(string email){     string url_set = "http://***.php?email=" + email;     string responsestr = "1";     try {         httpclient httpclient = new defaulthttpclient();         httppost httppost = new httppost(url_set);         httpclient.execute(httppost);         return responsestr;     } catch (unsupportedencodingexception e) {         e.printstacktrace();         return responsestr;     } catch (clientprotocolexception e) {         log.e("clientprotocolexception", "client");         e.printstacktrace();         return responsestr;     } catch (ioexception e) {         log.e("ioexception", "ioexption");         e.printstacktrace();         return responsestr;     } } 

you cannot make network call on main thread. need create separate thread run function.


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -