【问题标题】:Android cannot execute restful urlAndroid无法执行restful url
【发布时间】:2016-04-15 07:46:12
【问题描述】:

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

这是脚本

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

这就是我将请求发送到 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();
            }
        }



    });

}

清单

<?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>

【问题讨论】:

    标签: php android rest restful-url


    【解决方案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();
          }
       });
    }
    

    【讨论】:

    • 您好,感谢您的帮助。我试图按照你的方式写它,但似乎什么都没有发生。它也不会发回任何东西。会不会是主机服务器?
    • 您的清单中是否有互联网权限?,您的 java 代码是否在某种后台线程中运行?另外你为什么不在纯java中这样做,看起来你正在使用来自另一个sdk的某种方法?
    • 是的!以防万一,我也贴出来
    • 谢谢。无论如何,我只是使用一个基本的 url 流和一个缓冲的阅读器来读取服务器回复的内容。这就是我所做的。也许你可以解释一下你到底想要做什么?
    • 好吧,我正在尝试通过 asynchttp 中的 post 方法发送注册 ID(文本格式)。 php 脚本应该读取我的 POST 并创建一个包含数据的文本文件。我已经设法让整个代码在我的 mamp 服务器上运行,但是当我将它上传到 byethost 服务器时,它似乎根本不想生成文本文件。
    猜你喜欢
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 2021-12-15
    • 2015-06-19
    • 1970-01-01
    • 2014-05-14
    • 1970-01-01
    • 2023-01-04
    相关资源
    最近更新 更多