0

Hi I'm trying to send some parameters to my php server

I really want my php server to receive what user sended parameter and store them at mysql database

If I try to connect http://somesite/store.php?Location=somewhere&temp=something using chrome browser It worked well.

But If I try to use it on android.

No record is updated in my database

what is my problem?

URL testUrl;
        try {
            testUrl = new URL("http://somesite.com/gcm_server/getdata.php?Location=bathroom&Humidity=25%&Temperature=27&Brightness=45");

            HttpURLConnection conn = (HttpURLConnection)testUrl.openConnection();

            InputStream in = conn.getInputStream();

            InputStreamReader isw = new InputStreamReader(in);

            int data = isw.read();
            while (data != -1) {
                char current = (char) data;
                data = isw.read();
                System.out.print(current);
            }


            Log.d(TAG, "testUrl= " + testUrl.toString());

            conn.disconnect();
        }catch(MalformedURLException ex){
            Log.e(TAG, ex.toString());
        }

        catch(IOException e){
            Log.e(TAG, "url connection Error");
        }

If I try to connect then I got Log url connection Error but If I tried to connect somesite (I covered it with different site). I can connect using chrome browser

I don't know what is happening... Please hep me

kwony
  • 109
  • 1
  • 1
  • 5

2 Answers2

0

if you are using local webserver makesure you use 10.0.2.2 as server name instead of localhost .. Bcause android use localhost for emulator it self ..

Moinkhan
  • 12,732
  • 5
  • 48
  • 65
  • Try volley library . Which is used for networking task developed by google. And you don't have to worry about URLConnection and etc . – Moinkhan Jun 07 '15 at 07:01
  • I checked internet permission. I will try volley library. Thank you – kwony Jun 07 '15 at 07:54
0

OK. First of all. If ou are going to send stuff to a server and store it, you should really use a POST request instead of GET.

I have a small sample here to help you do that in Android:

    //These are your parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("param1","foo"));
    nameValuePairs.add(new BasicNameValuePair("param2","bar"));

    String result = "";
    InputStream is = null;
    HttpClient httpClient = new DefaultHttpClient();
    String urlPath = "http://somesite.com/somepage.php";

    try {
        HttpPost httpPost = new HttpPost(urlPath);
        httpPost.setEntity(new UrlEncodedFormEntity(params,     HTTP.UTF_8));
        BasicResponseHandler responseHandler = new BasicResponseHandler();
        result = httpClient.execute(httpPost, responseHandler);
    } catch (Exception e) {
        //Exception
    }
    //Your result is in the result variable now

This example is totally not complete and you need to import the things used, but hopefully you get what I'm doing.

You can get the params on the php server using $_POST["theparam"];

Hagbarth
  • 159
  • 7
  • Thanks but HttpPost is not supported anymore. I need to use variable URLconnection can you help me plz?? – kwony Jun 07 '15 at 06:28
  • Oh ok, sorry - did not realise that Maybe you can checkout this answer: http://stackoverflow.com/a/13486223/4579532 – Hagbarth Jun 07 '15 at 16:56