0

phpスクリプトとmysqlデータベースを使用してさまざまなユーザーにログインできるようにするAndroidアプリケーションがあります。JSONエラーのため、接続が確立されません。

PHPファイル:

<?php
$dbhost="localhost";
$dbuser="";
$dbpass="";
$dbdb="test";
$connect = mysql_connect($dbhost,$dbuser,$dbpass)or die("connection error");
mysql_select_db($dbdb)or die ("database selection error");
$username=isset($_POST['username']);
$password=isset($_POST['password']);
$query = mysql_query("SELECT * FROM table1 WHERE username = '$username' AND password = '$password'");
$num = mysql_num_rows($query);
if($num == 1) { while($list = mysql_fetch_assoc($query)){
$output=$list;
echo json_encode($output);
}
mysql_close();}?>

Androidコード:

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("username",username));
    nameValuePairs.add(new BasicNameValuePair("password",password));
    try {
        httpclient = new DefaultHttpClient() ;
        httppost = new HttpPost("http://10.0.2.2/projet/connection.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        response = httpclient.execute(httppost);
        Log.i("connect", response.getStatusLine().toString());
        //verifier requete http de code 200
        if(response.getStatusLine().getStatusCode()==200){
            entity = response.getEntity();
            //verifier que entity non null
            if(entity != null){
                InputStream instream = entity.getContent();
                //creer JSON object ayant converted data comme parametre
                JSONObject jsonresponse = new JSONObject(convertStreamToString(instream));
                //affecter json response à une variable locale 
                String retUser = jsonresponse.getString("username");
                String retpass = jsonresponse.getString("password");
                //valider login
                if (username.equals(retUser) && password.equals(retpass)){
                    Log.i("connect", response.getStatusLine().toString());
                    //creation de SharedPreference pour enregistrer les login details
                    SharedPreferences sp = getSharedPreferences("logindetails",0);
                    //modifier sharedPreferences
                    SharedPreferences.Editor spedit = sp.edit();
                    //mettre logindetails comme string
                    spedit.putString("username", username);
                    spedit.putString("password", password);
                    //fermer editor
                    spedit.commit();
                    appelle();
                    Toast.makeText(getBaseContext(), "connexion avec succés", Toast.LENGTH_SHORT).show();

                }else {
                    Toast.makeText(getBaseContext(), "login details invalide", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }catch(Exception e){
        e.printStackTrace();
        Toast.makeText(getBaseContext(), "Erreur lors du connexion", Toast.LENGTH_SHORT).show();
    }   
}

 private static String convertStreamToString(InputStream is) {
     //méthode pour parcourir la table et lire les données
        /*
         * To convert the InputStream to String we use the BufferedReader.readLine()
         * method. We iterate until the BufferedReader return null which means
         * there's no more data to read. Each line will appended to a StringBuilder
         * and returned as String.
         */
        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();
    }

JSONException:

07-31 11:03:36.995: W/System.err(342): org.json.JSONException: End of input at character 0 of 
4

1 に答える 1

0
        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
        HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
        HttpClient client = new DefaultHttpClient(httpParams);
        HttpPost request = new HttpPost( "http://www.colorssoftware.com/savedata.php");

        request.setHeader( "http://10.0.2.2/projet/connection.php" );       

        JSONObject json = new JSONObject();          
        json.put("user_name", "username");
        json.put("user_password", "password");

        Log.i("jason Object", json.toString());    
        System.out.println("jason Object value is" + json.toString());

        TextView text = (TextView)findViewById(R.id.textView);
        text.setText(json.toString());

        StringEntity se = new StringEntity(json.toString());                 
        se.setContentEncoding("UTF-8");
        se.setContentType("application/json");    
        request.setEntity(se);        

        HttpResponse response = client.execute(request);        
        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent();
        String _response = convertStreamToString(is);              
        int res_code = response.getStatusLine().getStatusCode();
于 2012-07-31T11:58:48.007 に答える