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