0

I work on an android app that should send a query parameter to a php code on server and the php code should retrieves the result and shows it for the user

So I need to a php code for this task.

JSONTransmitter

public class JSONTransmitter extends AsyncTask<JSONObject, JSONObject, JSONObject> {

    String url = " /..../Three.php";


    protected JSONObject doInBackground(JSONObject... data) {
        JSONObject json = data[0];
        HttpClient client = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(client.getParams(), 100000);
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());
        JSONObject jsonResponse = null;

        HttpPost post = new HttpPost(url);
        try {
            StringEntity se = new StringEntity("json="+json.toString());
            post.addHeader("content-type", "application/x-www-form-urlencoded");
            post.setEntity(se);

            HttpResponse response;
            response = client.execute(post);
            String resFromServer = org.apache.http.util.EntityUtils.toString(response.getEntity());

            jsonResponse=new JSONObject(resFromServer);
            Log.i("Response from server", jsonResponse.getString("msg"));

        } catch (Exception e) { e.printStackTrace();}

        return jsonResponse;
    }

}

MainActivity

 public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            JSONObject toSend = new JSONObject();
            toSend.put("msg", 1);

            JSONTransmitter transmitter = new JSONTransmitter();
            transmitter.execute(new JSONObject[] {toSend});

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

} 

This my php code trial

    <?php
if( isset($_POST["json"]) ) {
     $data = json_decode($_POST["json"]);
 mysql_connect("localhost", "root", "password") 
or die(mysql_error());
mysql_select_db("test") or die(mysql_error());


   $data  = mysql_query(" select  part_name from  Services_parts where  part_id=   $data->msg  ") 
or die(mysql_error());  



   echo json_encode($data);

 }

?>
5
  • 1
    So, what is the question? Commented Dec 28, 2015 at 8:41
  • The problem in my php code Commented Dec 28, 2015 at 8:43
  • REST is what you want gajotres.net/best-available-php-restful-micro-frameworks Commented Dec 28, 2015 at 8:44
  • What is the problem? What error are you getting? Commented Dec 28, 2015 at 8:45
  • I don't get the result Commented Dec 28, 2015 at 8:47

2 Answers 2

1

First of all, do not use the old mysql_... functions. I recommend you PDO, but if you want it the "oldish MySQL-style" at least use MySQLi. Also, since I assume you are not familiar with the security procedures, read about SQL Injections.

But let's get to your problem :)

You need to get the result from the query and pass it on to mysql_fetch_row or one of the other fetch functions.

$result = mysql_query("select...");
echo json_encode(mysql_fetch_row($result));
Sign up to request clarification or add additional context in comments.

3 Comments

Could you check if the browser returns anything if you output something in an else-statement? if(isset($_POST["json"])){ ... } else { echo "nothing;" }
Any suggestion please
I can give you a PDO solution, it's been years the last time I have used the old MySQL functions.
0

If you get nothing than you didn't sent your data right. Try to log your data JSONObject json = data[0]; to see what you get. Your php looks fine at first glance. When you test your code break it to smaller pieces. If you think your php is wrong then create yourself a html form above php code with input and button and then try it. If it works than it is ok. But here your problem is in android. Log your data and see what is going on. I'm not familiar with that JSONTransmiter I have never used it butt here is example that helped me with external base communication. http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/ . This is one perfect example clean and simple.

1 Comment

JSONObject json = data[0]; I get nothing

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.