Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android httppost/连接/登录问题_Android_Http Post - Fatal编程技术网

Android httppost/连接/登录问题

Android httppost/连接/登录问题,android,http-post,Android,Http Post,我在连接数据库时遇到问题。应用程序启动,一切正常,但总是返回我登录失败-连接问题 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.c

我在连接数据库时遇到问题。应用程序启动,一切正常,但总是返回我登录失败-连接问题

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()

}
有什么想法吗


Fanks无论如何。

根据您的代码,没有消息登录失败-连接问题,我认为测试的方法是使用Your's php文件,通过浏览器发送浏览器上的信息来证明它,然后寻找答案,以了解其是否采用相同的格式,在此之后,最好在if子句之前使用tooglebreakpoints,这样您就可以读取应用程序正在从数据库检索的值。如果你的日志中有错误,cat将很乐意发布它们。因此,我们可以进一步了解问题`}catch(异常e){e.printStackTrace();//在出现连接错误时显示toast//将消息更改为更友好的toast.makeText(getBaseContext(),“Login Unsuccessful.”,toast.LENGTH_SHORT).show()“我正在测试3.1蜂巢。
    <?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>