1

I want to pass some parameters to a php script sitting on a webserver. The url is http://tom.byethost3.com/gcmapp/gcm.php?shareRegId=true. On the php script side, it is supposed to detect with GET that shareRegId is not null and then gather the other information via POST. A text file will then be generated. I have tested this with my MAMP server and it works. However, whenever I try this from my webserver, nothing happens, I still am successful in contacting the GCM server and getting a registration ID but I can't seem to trigger the url at all.

This is the script

//Get Reg ID sent from Android App and store it in text file
if(!empty($_GET["shareRegId"])) {
    //$gcmRegID  = $_GET["shareRegId"]; 
    $gcmRegID  = $_POST["regID"]; 
    //echo $gcmRegID;
    file_put_contents("./gcmtest/GCMRegId.txt",$gcmRegID);
    echo "Done!";
    exit;
    }

This is how I send my request to the url

private void storeREG(){

    pg.show();
    params.put("regID", registerID);

    Log.d("STORE","STORE");
    //Make RESTful webservice call

    AsyncHttpClient client = new AsyncHttpClient();
    client.post(AppConstants.SERVER_URL,params,new AsyncHttpResponseHandler(){

        @Override
        public void onSuccess(String content) {
            pg.hide();
            if(pg!=null){
                pg.dismiss();
            }
            Toast.makeText(applicationCtx,"ID sharing successful",Toast.LENGTH_LONG).show();
            Intent home = new Intent(applicationCtx,HomeActivity.class);
            home.putExtra("regID",registerID);
            Log.d("REGID",registerID);
            startActivity(home);
            finish();
        }

        @Override
        public void onFailure(int statusCode, Throwable error, String content) {
            pg.hide();
            if(pg!=null){
                pg.dismiss();
            }
            Log.d("ERRORTHROW",error.toString());
            if(statusCode==404){
                Toast.makeText(applicationCtx,"Requested resource not found",Toast.LENGTH_LONG).show();
            }else if(statusCode==500){
                Toast.makeText(applicationCtx,"Something went wrong at the server",Toast.LENGTH_LONG).show();
            }else{
                Log.d("SHOWME",String.valueOf(statusCode));
                Toast.makeText(applicationCtx,"Unexpected error occurred",Toast.LENGTH_LONG).show();
            }
        }



    });

}

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.amosang.pushtest" >

<!-- GCM Permissions - Start here  -->

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />


<permission android:name="com.example.amosang.pushtest.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />


<uses-permission android:name="com.example.amosang.pushtest.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-permission android:name="android.permission.VIBRATE" />



<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".HomeActivity"
        android:label="@string/title_activity_home" >
    </activity>

    <receiver
        android:name=".GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >

        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <action android:name="com.example.amosang.pushtest" />
        </intent-filter>
    </receiver>

    <service android:name=".GCMNotificationIntentService" />
</application>



</manifest>

1 Answer 1

1

I was giving him an example of what i did, don't rate me down for helping :(

I was working on something very similar today, For an app project. Here is what I have done both PHP and Java:

<?php

$getname = $_GET['NAME'];
$name = strtolower($getname);

if(file_exists("files/$name")){
   echo "1";
} else{
   echo "0";
}

?>

And the Java code, I put it inside of a new thread by the way:

URL url = new URL("xxx.xxx.x.x/phpfile.php?NAME="+name);

InputStream in = url.openStream();

final BufferedReader bfr = new BufferedReader(new InputStreamReader(in));

String check = bfr.readLine();

if(check.equals("1")) {
   runOnUiThread(new Runnable() {
      @Override
      public void run() {
         Toast.makeText(MainActivity.this, "Sorry, But the file already exists!", Toast.LENGTH_LONG).show();
      }
   });
} else if(check.equals("0")) {
   runOnUiThread(new Runnable() {
      @Override
      public void run() {
           Toast.makeText(MainActivity.this, "File created!", Toast.LENGTH_LONG).show();
      }
   });
}
Sign up to request clarification or add additional context in comments.

7 Comments

Hello, thanks for your help. I tried to write it your way but nothing seems to be happening. It doesn't send anything back as well. Could it be the host server?
Do you have the internet permission in your manifest?, Is the java code you have running in some kind of background thread? Also why are you not doing this in pure java, it looks like you are using some kind of methods from another sdk?
Yup! Ill post it up as well just in case
Thanks. anyway, I'd just use a basic url stream and a buffered reader to read what the server reply's back. Thats what i did. Maybe you could explain a bit more of what you are exactly trying to do?
Well I'm trying to send a registration id (text format) via the post method in asynchttp. The php script is supposed to read my POST and create a text file with the data in it. I have managed to get this entire code running on my mamp server but when i uploaded it to a byethost server, it doesn't seem to want to generate a text file at all.
|

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.