0

I'm trying to make a login button work, my aim is to : send web request to server with username and password(in md5), receive the response from the server(the response is in xml), save the response to a file or something and use it for the next requests. My code is similar to this :

Button login_button = (Button) findViewById(R.id.button_login);
if (login_button != null) {
login_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText username_text =(EditText)findViewById(R.id.username);
String username_string = username_text.getText().toString();
EditText password_text = (EditText) findViewById(R.id.password);
String password_string = password_text.getText().toString();
String md5_pass=MD5.crypt(password_string);  // MD5 is a class that encrpts in md5                            
//File tempFile = new File(Envoirment,"tempFile");
 new JSONTask().execute("http://site/Security.php?function=login&user=\" + username_string + \"&hash=\" + md5_pass");
                }
        });
    }
public class JSONTask extends AsyncTask<String,String,String> 
{
    @Override
    protected String doInBackground(String... params) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;
        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();
            InputStream stream = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(stream));
            String line;
            StringBuffer buffer = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
            return buffer.toString();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    @Override
    protected void onPostExecute(String result) {                               
        super.onPostExecute(result);
        Intent i = new Intent(Login.this, MainActivity.class);
        startActivity(i);
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(".\\tempFile"));
            //writer.write(); i don't know how
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
        /*else {
            Intent i = new Intent(LoginActivity.this, RegisterActivity.class);
            startActivity(i);
        }*/
}

My problem appears to be in the AsyncTask. The web response will have a token that i need to use for further requests. I get this :

Process: try.app, PID: 17322
                                                       java.lang.RuntimeException: An error occured while executing doInBackground()
                                                           at android.os.AsyncTask$3.done(AsyncTask.java:300)
                                                           at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
                                                           at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
                                                           at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                           at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                                                           at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                           at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                           at java.lang.Thread.run(Thread.java:841)
                                                        Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
                                                           at java.net.InetAddress.lookupHostByName(InetAddress.java:464)
                                                           at java.net.InetAddress.getAllByNameImpl(InetAddress.java:251)
                                                           at java.net.InetAddress.getAllByName(InetAddress.java:229)
                                                           at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
                                                           at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
                                                           at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
                                                           at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
                                                           at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
                                                           at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
                                                           at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
                                                           at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
                                                           at try.app.Login$JSONTask.doInBackground(Login.java:67)
                                                           at try.app.Login$JSONTask.doInBackground(Login.java:57)
                                                           at android.os.AsyncTask$2.call(AsyncTask.java:288)
                                                           at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                           at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
                                                           at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                                           at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                                                           at java.lang.Thread.run(Thread.java:841) 
                                                        Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
                                                           at libcore.io.Posix.getaddrinfo(Native Method)
                                                           at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
                                                           at java.net.InetAddress.lookupHostByName(InetAddress.java:451)
                                                           at java.net.InetAddress.getAllByNameImpl(InetAddress.java:251) 
                                                           at java.net.InetAddress.getAllByName(InetAddress.java:229) 
                                                           at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28) 
                                                           at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216) 
                                                           at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122) 
                                                           at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292) 
                                                           at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255) 
                                                           at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206) 
                                                           at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345) 
                                                           at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89) 
                                                           at try.app.Login$JSONTask.doInBackground(Login.java:67) 
                                                           try.app.Login$JSONTask.doInBackground(Login.java:57) 
                                                           at android.os.AsyncTask$2.call(AsyncTask.java:288) 
                                                           at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
                                                           at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
                                                           at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                                           at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                                                           at java.lang.Thread.run(Thread.java:841) 
                                                        Caused by: libcore.io.ErrnoException: getaddrinfo failed: EACCES (Permission denied)

I have these in the manifest :

<uses-permission android:name="android.permissions.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

1 Answers1

0

First, you don't need a token from the server to pass back for further requests. The user id will be sufficient for that. The server can check whether there is a proper session for that user, quite the same way it would check for the validity of the token.

Now your problem might be where to store the information, whether user id or token, in your app so that it will be available for further requests. If that's what you want to know, check my answer to How to: Keep track of Progress on questionnaire.

(Reviewers: Please don't flag this question as a duplicate (yet), as it's not clear whether that's the core of the question.)

Community
  • 1
  • 1
TAM
  • 1,731
  • 13
  • 18