1

我在连接数据库时遇到问题。应用程序启动,一切似乎都很好,但总是返回我登录不成功 - 连接问题。

package kur.jie;

import java.io.InputStream;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import kur.jie.R;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class jieActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
       EditText etUser, etPass;
           Button bLogin;
        // Create string valuables
       String username, password;
       HttpClient httpclient;

       HttpPost httppost;
       ArrayList<NameValuePair> NameValuePairs;
       HttpResponse response;
       HttpEntity entity;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);          
            initialise();
        }

        private void initialise() {
            etUser = (EditText) findViewById(R.id.etUser);
            etPass = (EditText) findViewById(R.id.etPass);
            bLogin = (Button) findViewById(R.id.bSubmit);
            //Now to set an onClickListener
            bLogin.setOnClickListener(this);
        }

        public void onClick(View v) {
            httpclient = new DefaultHttpClient();
            httppost  = new HttpPost("http://www.xxx.lt/app/login.php");

            username = etUser.getText().toString();
            password = etPass.getText().toString();         


            try {

                NameValuePairs = new ArrayList<NameValuePair>();

                NameValuePairs.add(new BasicNameValuePair("username",        username));
                NameValuePairs.add(new BasicNameValuePair("password", password));


                httppost.setEntity(new UrlEncodedFormEntity(NameValuePairs));

                response = httpclient.execute(httppost);

                if  (response.getStatusLine().getStatusCode()==200){
                    entity = response.getEntity();

                if (entity != null) {

                    InputStream instream = entity.getContent();

                    JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));

                    String retUser= jsonResponse.getString("user");
                    String retPass= jsonResponse.getString("pass");

                    if (username.equals(retUser) && password.equals(retPass)) {

                        SharedPreferences sp = getSharedPreferences("logindetails", 0);
                        SharedPreferences.Editor spedit = sp.edit();
                        spedit.putString("user", username);
                        spedit.putString("pass", password);
                        spedit.commit();

                        Toast.makeText(getBaseContext(), "Success", Toast.LENGTH_SHORT).show();


                    } else {

                    Toast.makeText(getBaseContext(), "Invalid Login", Toast.LENGTH_SHORT).show();

                    }

                }
                }

            } catch(Exception e){
                e.printStackTrace();
                //Display toast when there is a connection error
                //Change message to something more friendly
                Toast.makeText(getBaseContext(), "Login Unsuccessful.", Toast.LENGTH_SHORT).show();


                }
            }

        private static String convertStreamToString(InputStream is) {

            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();

            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return sb.toString();
        }//END convertStreamToString()

}

我的 php 重新运行 json{"id":"1","user":"xxxx@xxx.com","pass":"xxxx","long":"54.74086","lat":"25.27145","stop_id":"1","status":"+"}

我正在记录来自 php 文件的所有数据,但来自应用程序的任何数据。

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="kur.jie"
    android:versionCode="1"
    android:versionName="1.0" >

   <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="12" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".jieActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

有任何想法吗?

反正粉丝..

4

0 回答 0