0

I want something like that.i have a button and 2 textbox when user put their username and password and then click on login button then login action will be performed and then welcome the user and go to another page .my code was like that:

try {
                Connection.Response res = Jsoup.connect("URL")
                        .data("log", "abcd", "pwd", "12345", "wp-submit", "প্রবেশ", "redirect_to", "url", "testcookie", "1")
                        .method(Method.POST)
                        .execute();
                Map<String, String> cookies = res.cookies();

                Document doc2 = Jsoup
                    .connect("new_url")
                    .cookies(cookies)
                    .get();

                s = doc2.text().toString();
                t.setText(s);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                t.setText("no");
            }

But login action cant be successful and here it always shows "no". How can i successfully do that???

ImonBayazid
  • 1,066
  • 3
  • 16
  • 41
  • added proper permissions in manifest file ? – Usman Kurd Feb 20 '13 at 08:42
  • i added this to manifest file: – ImonBayazid Feb 20 '13 at 08:47
  • debug your code and check logcat what it says – Usman Kurd Feb 20 '13 at 08:48
  • give us your `import`s – Shoshi Feb 20 '13 at 09:06
  • I am a beginner in android and i cant understand the debugging here properly .. – ImonBayazid Feb 20 '13 at 09:07
  • import java.io.IOException; import java.util.Map; import org.jsoup.Connection; import org.jsoup.Connection.Method; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; @Shoshi – ImonBayazid Feb 20 '13 at 09:08
  • so u r getting `no`. that mean, u r in `catch{}` so do this: `Log.e("tag", e.toString());`. it will show some error in `LogCat`. copy them and give here. @user2029069 – Shoshi Feb 20 '13 at 09:12

1 Answers1

0

i think it is threading problem. because your internet request is on main UI-Thread. test this:

private class AsyncExecution extends AsyncTask<Void, Void, Void>{
    boolean tracker = false;
    String s = "";
    @Override
    protected Void doInBackground(Void... params) {
       try {
           Connection.Response res = Jsoup.connect("http://www.kuetlive.com/wp-login.php")
                        .data("log", "abcd", "pwd", "12345", "wp-submit", "প্রবেশ", "redirect_to", "http://www.kuetlive.com/wp-admin/", "testcookie", "1")
                        .method(Method.POST)
                        .settimeout(60000)//time set for the connection 1 min
                        .execute();
                Map<String, String> cookies = res.cookies();

                Document doc2 = Jsoup
                    .connect("http://www.kuetlive.com/wp-admin/profile.php")
                    .cookies(cookies)
                    .get();

                s = doc2.text().toString();
                tracker = true;

       } catch (Exception e) {
           // TODO Auto-generated catch block
           Log.e("tag", e.toString());
           tracker = false;

       }
     }
  --- // }  // --- i add this by mistake, delete this

    @Override
    protected void onPostExecute(Void result) {
       if(tracker){
            t.setText(s);
       }else{
            t.setText("no");
       }
    }

}

and call it as

new AsyncExecution().execute();

Edited: you cannot perform an internet request on main UI-Thread. that is why u need a different thread. now u could implement a simple thread instead of AsyncTask. then why i suggest u to use AyncTask. the answer is, u cannot update your UI from a simple Thread. that is why u need AsyncTask, because AsyncTask gives u flexibility to update UI but execute your method in different Thread beside UI-Thread.

Shoshi
  • 2,254
  • 1
  • 29
  • 43
  • Don't use a normal thread for this. Use AsyncTask. Big chance you cannot set the text of t. – RvdK Feb 20 '13 at 09:20
  • my mistake, i totally have forgot that. i am editing my answer @RvdK – Shoshi Feb 20 '13 at 09:22
  • have i make a new class name "AsyncExecution " and then have i to call it as new AsyncExecution().execute(); from my MainActivity's onCreate???isn't it??@Shoshi – ImonBayazid Feb 20 '13 at 09:45
  • no no no, u should create this class as inner class. like, in MainActivity right after the OnCreate() method create the class, this would be much easier than creating a new class @user2029069 – Shoshi Feb 20 '13 at 09:48
  • what should be here-> doInBackground(Void... params) ?? and i m also getting an error of using this @Override protected void onPostExecute(Void result) { if(tracker){ t.setText(s); }else{ t.setText("no"); } } – ImonBayazid Feb 20 '13 at 09:58
  • please consider that i n beginner in android @Shoshi – ImonBayazid Feb 20 '13 at 10:01
  • I tried your previous code and on LogCat under tag it shows "java.net.SocketTimeoutException"@Shoshi – ImonBayazid Feb 20 '13 at 10:07
  • @user2029069 : i have edited my answer. an extra `}` was added by my mistake. u should consider that i am writing this code here, not on IDE :) . also remember that `doInBackground` and `onPostExecute` methods are under `AsyncExecution` class. and the parameter of `doInBackground` and `onPostExecute` are `Void... params` and `Void result` respectively . – Shoshi Feb 20 '13 at 10:10
  • i understand.have i put it "new AsyncExecution().execute();" on my login button onclicklistener ??? – ImonBayazid Feb 20 '13 at 10:24
  • have i call when the user click on login button i mean have i call it like "new AsyncExecution().execute();" in the loginbutton onclicklistner ???? – ImonBayazid Feb 20 '13 at 10:27
  • There is an error on the line "doInBackground(Void... params)"?? It says this result must return a result of type Void so i added "return null;" after Catch statement.Then i have run the app but it still shows "no". please help me about this coz i have tried and Googled many times for it.If u have time please do me a favour @Shoshi – ImonBayazid Feb 20 '13 at 13:16
  • if it still says no, then it should print some error in your logcat. i have wrote the `Log.e("tag", e.toString());` statement to print the error in logcat. post your logcat – Shoshi Feb 20 '13 at 13:54
  • it says java.net.SocketTimeoutException – ImonBayazid Feb 20 '13 at 13:59
  • @user2029069 : i have set a timeout in the Jsoup Connection. check out answer. if it doesn't help u should turn off your firewall in pc, but keep this change(settimeout) in your code – Shoshi Feb 20 '13 at 14:08
  • Thanx a lot ...yeah in this time it shows something.Could you tell what was wrong with me??Why AsyncExecution and others things were needed here??@Shoshi – ImonBayazid Feb 20 '13 at 14:33
  • 1
    i have added the explanation in my answer. check @user2029069 – Shoshi Feb 20 '13 at 14:43