1

this code is working perfectly...but I have no idea on how to manipulate the result I get. Right here I am just sending from the application to the website with a simple query

<?php
$con = mysql_connect("","user","pass") or die(mysql_error());
$objDB = mysql_select_db("tablename", $con) or die(mysql_error()); 

$username = mysql_real_escape_string($_POST['user1']); 
$sql = mysql_query("SELECT * FROM doctor WHERE username = '$username' ");

 while($row=mysql_fetch_assoc($sql)) 
 $output[]=$row;
  print(json_encode($output));
  mysql_close();
 ?>      

Here is my Android code:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //Saved preferences
        app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
        button = (Button)findViewById(R.id.Button01);
        username= (EditText)findViewById(R.id.EditText01);//et
        password= (EditText)findViewById(R.id.EditText02);//et
        echoresponse= (TextView)findViewById(R.id.tv);//tv

     //   String str_user = app_preferences.getString("username", "0");
     //   String str_pass = app_preferences.getString("password", "0");
     //   String str_check = app_preferences.getString("checked", "0");

     //   if(str_check.equals("yes"))
     //   {
     //     username.setText(str_user);
     //     //password.setText(str_pass); 
     //   }

        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                try{
                    httpclient=new DefaultHttpClient();
                    httppost= new HttpPost("http://www.baemer.com/androiddealer.php"); // make sure the url is correct.
                    //add your data

                    nameValuePairs = new ArrayList<NameValuePair>(2);
                    // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
                    nameValuePairs.add(new BasicNameValuePair("user1",username.getText().toString().trim()));  // $Edittext_value = $_POST['Edittext_value'];
                    nameValuePairs.add(new BasicNameValuePair("pass1",password.getText().toString().trim()));  // $Edittext_value = $_POST['Edittext_value'];                   

                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    //Execute HTTP Post Request
                    response=httpclient.execute(httppost);
                    // edited by James from coderzheaven.. from here....
                    ResponseHandler<String> responseHandler = new BasicResponseHandler();
                    String res = httpclient.execute(httppost, responseHandler);
                    System.out.println("Response : " + res);
                    echoresponse.setText("Response from PHP : " + res);

                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();
                }catch(Exception e){
                    System.out.println("Exception : " + e.getMessage());
                }

               //convert response to string

                try{

                    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"),8);
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    while((line = reader.readLine())!=null){
                        sb.append(line+"\n");
                    }
                    is.close();
                    result=sb.toString();
                }catch(Exception e){
                    System.out.println("Exception : " + e.getMessage());//if not try Log. from http://www.gaanza.com/blog/how-to-mysql/
                }

                //parse JSON data

                try{
                    JSONArray jArray = new JSONArray(result);
                    for(int i =0;i<jArray.length();i++){
                        JSONObject json_data = jArray.getJSONObject(i);

                        Log.i("log_tag","id: "+json_data.getInt("dr_serial")+
                                ", username: "+json_data.getString("username"));

                    }
                }catch(JSONException e){
                    System.out.println("Exception : " + e.getMessage());//if not try Log. from http://www.gaanza.com/blog/how-to-mysql/
                }


            }


        });
    }
}

But right now, when I press the button it displays all the tables are said in Log.i(.....blabla). How can I manipulate those columns ? As in for example: assign some variable dr_serial to then display it on a textview or something.

1
  • Can you show the response string from the server? Commented Nov 10, 2011 at 8:06

1 Answer 1

1

Just call

int dr_serial = json_data.getInt("dr_serial");

Then you can do whatever you want with it.

Sign up to request clarification or add additional context in comments.

1 Comment

Log.i writes (logs) whatever parameters you've passed to the method to the logcat. You can see this in Eclipse by opening the DDMS perspective after you've run your code in the emulator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.