Android无法执行restful url

时间:2016-04-15 07:46:13

标签: php android rest restful-url

我想将一些参数传递给坐在网络服务器上的php脚本。网址为http://tom.byethost3.com/gcmapp/gcm.php?shareRegId=true。 在php脚本方面,它应该用GET检测到shareRegId不为null,然后通过POST收集其他信息。然后将生成文本文件。 我用我的MAMP服务器对它进行了测试,它可以工作。但是,每当我从我的网络服务器上尝试此操作时,没有任何反应,我仍然能够成功联系GCM服务器并获得注册ID,但我似乎无法触发该网址。

这是脚本

//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;
    }

这是我将请求发送到网址的方式

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();
            }
        }



    });

}

清单

<?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 个答案:

答案 0 :(得分:1)

我正在给他一个我所做的事情的例子,不要因为我的帮助而拒绝我:(

我今天正在研究一些非常相似的东西,对于一个应用项目。这是我用PHP和Java做的:

<?php

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

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

?>

和Java代码一样,我把它放在一个新线程中:

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();
      }
   });
}