安卓应用程序中的登录按钮,更正与服务器的通信



我正在尝试让登录按钮工作,我的目标是:使用用户名和密码(在md5中)将web请求发送到服务器,从服务器接收响应(响应在xml中),将响应保存到文件或其他文件中,并用于下一个请求。我的代码类似于此:

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);
        }*/
}

我的问题似乎出现在AsyncTask中。web响应将有一个令牌,我需要使用它来进行进一步的请求。我得到这个:

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)

清单上有这些:

<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"/>

首先,您不需要来自服务器的令牌来传递给进一步的请求。用户id就足够了。服务器可以检查该用户是否有合适的会话,就像检查令牌的有效性一样。

现在你的问题可能是在你的应用程序中存储信息的位置,无论是用户id还是令牌,这样它就可以用于进一步的请求。如果这是你想知道的,请查看我对"如何:跟踪进度"问卷的回答。

(评审员:请不要将此问题标记为重复问题,因为尚不清楚这是否是问题的核心。)

相关内容

  • 没有找到相关文章

最新更新