1. Are you ready for the Galaxy S20? Here is everything we know so far!

Android CMS

Discussion in 'Android Lounge' started by acap89, Apr 13, 2012.

  1. acap89

    acap89 Lurker
    Thread Starter

    im developing android cms that synchronize the database from server to client. the interface are already there but, there some bugs that i cannot link to the server

    here my coding
    Code (Text):
    1. public void onClick(DialogInterface arg0, int arg1) {
    2.                  
    3.                     // Get the username from the user
    4.                      ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
    5.                     postParameters.add(new BasicNameValuePair("Username", UserName.getText().toString()));
    6.                    
    7.                    
    8.                     String response = null;
    9.                    
    10.                     try {
    11.                         response = CustomHttpClient.executeHttpPost(ServerIP+"LMS/LMSscripts/FirstTimeUser.php", postParameters);  //Enetr Your remote PHP,ASP, Servlet file link
    12.                         String res=response.toString().substring(0,1);
    13.                        
    14.                         // res = res.trim();
    15.                         res= res.replaceAll("\\s+","");
    16.                                 //error.setText(res);
    17.  
    18.                             if(res.equals("1")) {
    19.                                 String GetUsername = UserName.getText().toString();
    20.                                 String UserFile = ServerIP + "LMS/LMSscripts/" + GetUsername + ".txt";
    21.                                
    22.                                 // Make the user directory
    23.                                  
    24.                                 File userDirectory = new File(SDroot.concat(GetUsername));
    25.                                 userDirectory.mkdirs();
    26.                                
    27.                                 try {
    28.                                    
    29.                                     // Create a URL to read the student's text file
    30.                                     URL url = new URL(UserFile);
    31.  
    32.                                     // Read all the text returned by the server
    33.                                     // fileName is refer to the name of the file
    34.                                     BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    35.                                     String courseCode;
    36.                                     // Create the course directory
    37.                                     while ((courseCode = in.readLine()) != null) {
    38.                                        
    39.                                         File courseDirectory = new File(SDroot.concat(GetUsername),"/".concat(courseCode));
    40.                                         courseDirectory.mkdirs();
    41.                                        
    42.                                     }
    43.                                     in.close();
    44.  
    45.                                 } catch (MalformedURLException e) {
    46.                                 } catch (IOException e) {
    47.                                 }
    48.                                
    49.                                
    50.                             }
    51.                             else {
    52.                                 //incorrect
    53.                                 // print on the toastbox on the screen
    54.                                 // the button was clicked
    55.                                 Toast.makeText(getApplicationContext(), "No username found!", Toast.LENGTH_LONG).show();
    56.                             }
    57.                     } catch (Exception e) {
    58.                             UserName.setText(e.toString());
    59.                     }
    60.                
    61.               }
    62.                
    63.             });
    when i enter the username, it state that "No username found" but the username are there in the server.

    please give me some suggestion on how to debug on this..

    this is my FirstTimeUser.php
    Code (Text):
    1. <?php
    2.  
    3.  $un = $_POST['Username'];
    4.  
    5.  // connect to the db
    6.  
    7.  $mysql_host = "localhost";
    8.  $mysql_database = "*****";
    9.  $mysql_user = "******";
    10.  $mysql_password = "****";
    11.  
    12.  $conn = mysql_connect($mysql_host,$mysql_user,$mysql_password);
    13.  mysql_select_db($mysql_database,$conn);
    14.  
    15.  $query = "SELECT * FROM student WHERE Username='$un'";
    16.  $result = mysql_query($query);
    17.  
    18.   //Store all the course code into an array
    19.  $course_code = mysql_fetch_array($result);
    20.  
    21.  //verification
    22.  if(mysql_num_rows($result) == 1) {
    23.     echo 1; // for correct login response
    24.    
    25.     $format = ".txt";
    26.     $output = $un.$format;
    27.     $fh = fopen($output,'w') or die ("Can't open file");
    28.    
    29.  
    30.     for($num=1;$num<=5;$num++) {
    31.       $code = $course_code['Course'.$num];
    32.       fwrite($fh,$code."\r\n");
    33.     }
    34.     chmod($output,0777); //chmod the file to 0777
    35.     fclose($fh);
    36.   } else {
    37.  
    38.     echo 0; // for incorrect login response
    39.   }
    40. ?>
     



    1. Download the Forums for Android™ app!


      Download

       
  2. GeorgeN

    GeorgeN Well-Known Member

    Are you sure HttpResponse.toString() gets the data returned by the server? I would expect it to just give you some mangled object name - try printing the value and seeing what you get in logcat:
    Code (Text):
    1. Log.d("CMS",response.toString());
    I suspect you will need some extra code that does something like this:
    Code (Text):
    1. HttpEntity entity = response.getEntity();
    2. InputStream is = entity.getContent();
    And then you can read your data from the InputStream object.

    -George
     
Loading...
Similar Threads - Android CMS
  1. Dw1892
    Replies:
    2
    Views:
    398
  2. The_Chief
    Replies:
    4
    Views:
    376
  3. 20GT
    Replies:
    7
    Views:
    497
  4. Joshua Jackson
    Replies:
    2
    Views:
    423
  5. Veez999
    Replies:
    6
    Views:
    729
  6. Wookbert
    Replies:
    2
    Views:
    688
  7. Mr Padh
    Replies:
    4
    Views:
    482
  8. Jelyob
    Replies:
    12
    Views:
    793
  9. Md Saruar Hossain
    Replies:
    4
    Views:
    540
  10. Karman15
    Replies:
    1
    Views:
    413

Share This Page

Loading...