2012-07-31 3 views
0

PHP 스크립트와 MySQL 데이터베이스를 사용하여 다른 사용자를 로그인 할 수있게하는 안드로이드 애플리케이션이 있습니다. JSON 오류로 인해 연결이 설정되지 않았습니다.mysql과 안드로이드 연결을위한 JSONException

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();}?> 

안드로이드 코드 :

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 

답변

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(); 
+0

미안하지만 난 내가해야 할 어디서 무엇인지 이해하지 못하는 내 코드의 문제 ?? – amalch