【问题标题】:Android app unable to post details to urlAndroid 应用无法将详细信息发布到 url
【发布时间】:2016-04-14 04:22:30
【问题描述】:

我有一个发布到“http://jont.byethost3.com/gcmtest/gcm.php?shareRegId=true”的 Android 应用。一旦被访问,应用程序会向脚本发布一个 id 密钥,并且 php 文件将其保存到我的文件服务器上的文本文件中。 我已经在浏览器中手动输入了 url,由于没有收到任何 POST 数据,它成功生成了一个空文本文件,但是当我在我的应用程序上测试它时,它不起作用。

我使用 Asynchttp 像这样调用和传递数据

private void backgroundRegister(final String username){
    new AsyncTask<Void, Void, String>(){

        @Override
        protected String doInBackground(Void... params) {
            String msg = "";
            try{

                if(gcmObj==null){
                    gcmObj = GoogleCloudMessaging.getInstance(getApplicationContext());
                }
                registerID = gcmObj.register(AppConstants.PROJ_ID);
                msg = "Registration ID : " + registerID;
                Log.d("REGOID",registerID);

            }catch(IOException e){
                Log.d("IOEXCEPTION",e.toString());
            }
            return msg;
        }
        @Override
        protected void onPostExecute(String s) {
            if(!TextUtils.isEmpty(registerID)){
                savetoSharedPrefs(getApplicationContext(),registerID,username);
                Toast.makeText(getApplicationContext(),"Registered with GCM",Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(getApplicationContext(),"Registration failed",Toast.LENGTH_LONG).show();
            }
        }
    }.execute(null, null, null);
}

private void savetoSharedPrefs(Context context, String registerID,String userName){
    SharedPreferences prefs = getSharedPreferences("userDetails", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(regID, registerID);
    editor.putString(userNAME, userName);
    editor.commit();
    storeREG();
}

//store in the file server (PHP)
private void storeREG(){

    pg.show();
    params.put("regID", registerID);
    //Make RESTful webservice call

    AsyncHttpClient client = new AsyncHttpClient();
    client.post(AppConstants.SERVER_URL,params,new AsyncHttpResponseHandler(){
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            pg.hide();
            if(pg!=null){
                pg.dismiss();
            }
            Toast.makeText(getApplicationContext(),"ID sharing successful",Toast.LENGTH_LONG).show();
            Intent home = new Intent(getApplicationContext(),HomeActivity.class);
            home.putExtra("regID",registerID);
            Log.d("REGID",registerID);
            startActivity(home);
            finish();
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
            pg.hide();
            if(pg!=null){
                pg.dismiss();
            }
            if(statusCode==404){
                Toast.makeText(getApplicationContext(),"Requested resource not found",Toast.LENGTH_LONG).show();
            }else if(statusCode==500){
                Toast.makeText(getApplicationContext(),"Something went wrong at the server",Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(getApplicationContext(),"Unexpected error occurred",Toast.LENGTH_LONG).show();
            }
        }


    });

}

我要传递的数据只是纯文本。 我希望有人可以帮助我。

编辑

我的 php 脚本

if(!empty($_GET["shareRegId"])) {
    $gcmRegID  = $_POST["regID"]; 
    //echo $gcmRegID;
    file_put_contents("./GCMRegId.txt",$gcmRegID);
    echo "Done!";
    exit;
    }

我的清单以防万一

 <?xml version="1.0" encoding="utf-8"?>

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

更新: 我已经设法使用我的 mamp 服务器让这个代码在本地工作,当我将它上传到这个主机时问题仍然存在。

【问题讨论】:

  • 你是如何在这里传递参数的?通过附加到 url ?
  • 您通过附加到 url 和邮寄方式向 URL 传递数据?这就是我猜的问题
  • 您好,感谢您的回复。我在问题中编辑了我的代码。我通过 url 旁边的参数传递数据
  • 我还没有设法解决这个问题。使用 Postman 或 Restful 测试客户端进行任何形式的测试都会返回 200 状态码,表示 OK。

标签: java php android restful-url


【解决方案1】:

为了使用AsyncAndroidHttp 库传递发布参数,您可以使用提供的RequestParams 类并将其传递给您的Post API 调用,我的意思的一个小演示:

AsyncHttpClient client = new AsyncHttpClient();
RequestParams req = new RequestParams();
req.add("shareRegId","true");

client.post(AppConstants.SERVER_URL,req,new AsyncHttpResponseHandler(){
    @Override
    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
        pg.hide();
        if(pg!=null){
            pg.dismiss();
        }
        Toast.makeText(getApplicationContext(),"ID sharing successful",Toast.LENGTH_LONG).show();
        Intent home = new Intent(getApplicationContext(),HomeActivity.class);
        home.putExtra("regID",registerID);
        Log.d("REGID",registerID);
        startActivity(home);
        finish();
    }

这将帮助您通过Post Params

【讨论】:

  • 我已经添加了我的 php 脚本。它应该使用 GET 来检查 shareRegId 是否为空,然后从 POST 中获取数据。到目前为止,它似乎根本没有联系该网站。
  • 问题是我不知道client.post是否能够发送除了在php端创建的文本文件之外的任何信息。目前我所有的权限都是正确的,但没有生成文本文件
  • 暂时做一件事,将您的post 更改为get 并在php 脚本中使用$_GET 来获取这两个参数并看看会发生什么
  • 我改了,尝试用get。但是文件服务器似乎没有从我的应用程序接收任何连接。结果相同。没啥事儿。我的清单已设置为允许上网。但是,当我复制该网址并将其粘贴到我的浏览器时,可以创建该文件。
  • 覆盖AsyncHttpResponseHandler onFailure方法,看看是否返回错误
猜你喜欢
  • 2014-07-23
  • 2015-10-07
  • 2018-09-14
  • 1970-01-01
  • 2017-09-24
  • 2021-03-24
  • 1970-01-01
  • 2020-08-18
  • 1970-01-01
相关资源
最近更新 更多