Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从Android向php发送对象_Java_Php_Android - Fatal编程技术网

Java 从Android向php发送对象

Java 从Android向php发送对象,java,php,android,Java,Php,Android,我对Android非常陌生,我想将一些变量从我的Android应用程序传递到php页面。我使用了一个页面中的代码,并试图使其适应我的需要,但目前还没有传递变量。我想做的是,id为“button_wp”的按钮激活函数“run”。我使用的代码如下所示: Android xml <Button android:layout_width="wrap_content" android:layout_height="wrap_content" andro

我对Android非常陌生,我想将一些变量从我的Android应用程序传递到php页面。我使用了一个页面中的代码,并试图使其适应我的需要,但目前还没有传递变量。我想做的是,id为“button_wp”的按钮激活函数“run”。我使用的代码如下所示:

Android xml

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go to webpage"
        android:id="@+id/button_wp"
        android:onClick="wp" />
php

$json = file_get_contents('php://input');
$obj = json_decode($json);
$precio = $obj->{'precio'};
调试后的错误:

08-18 04:28:02.021 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve interface method 16209: Landroid/view/Window$Callback;.onProvideKeyboardShortcuts (Ljava/util/List;Landroid/view/Menu;I)V

08-18 04:28:02.021 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)

08-18 04:28:02.021 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve interface method 16211: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z

08-18 04:28:02.021 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve interface method 16215: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;

08-18 04:28:02.021 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve virtual method 532: Landroid/content/res/TypedArray;.getChangingConfigurations ()I

08-18 04:28:02.021 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve virtual method 554: Landroid/content/res/TypedArray;.getType (I)I

08-18 04:28:02.211 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve virtual method 16639: Landroid/widget/FrameLayout;.startActionModeForChild (Landroid/view/View;Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;

08-18 04:28:02.271 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve virtual method 495: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;

08-18 04:28:02.271 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve virtual method 497: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;

08-18 04:28:02.271 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve virtual method 321: Landroid/content/Context;.getColorStateList (I)Landroid/content/res/ColorStateList;

08-18 04:28:02.651 31463-31463/com.example.+++.+++E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering

08-18 04:28:02.651 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve instanceof 152 (Landroid/graphics/drawable/RippleDrawable;) in Landroid/support/v7/widget/AppCompatImageHelper;

08-18 04:35:38.731 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve virtual method 16912: Landroid/widget/Spinner;.getPopupContext ()Landroid/content/Context;

08-18 04:35:38.731 31463-31463/com.example.+++.+++E/dalvikvm: Could not find class 'android.widget.ThemedSpinnerAdapter', referenced from method android.support.v7.widget.AppCompatSpinner$DropDownAdapter.<init>

08-18 04:35:38.731 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve instanceof 2085 (Landroid/widget/ThemedSpinnerAdapter;) in Landroid/support/v7/widget/AppCompatSpinner$DropDownAdapter;

08-18 04:37:23.001 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve virtual method 196: Landroid/app/Notification$Builder;.setLocalOnly (Z)Landroid/app/Notification$Builder;

08-18 04:37:23.001 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve virtual method 453: Landroid/content/pm/PackageManager;.getPackageInstaller ()Landroid/content/pm/PackageInstaller;

08-18 04:37:32.901 31463-31463/com.example.+++.+++W/dalvikvm: method Landroid/support/v7/widget/ListViewCompat;.lookForSelectablePosition incorrectly overrides package-private method with same name in Landroid/widget/ListView;

08-18 04:37:32.901 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve virtual method 14245: Landroid/support/v7/widget/DropDownListView;.drawableHotspotChanged (FF)V

08-18 04:37:32.901 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve virtual method 15841: Landroid/view/View;.drawableHotspotChanged (FF)V

08-18 04:37:39.821 31463-31463/com.example.+++.+++W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.
08-18 04:28:02.021 31463-31463/com.example.++++++W/dalvikvm:VFY:无法解析接口方法16209:Landroid/view/Window$Callback;。onProvideKeyboardShortcuts(Ljava/util/List;Landroid/view/Menu;I)V
08-18 04:28:02.021 31463-31463/com.example.++++++W/dalvikvm:VFY:找不到签名中引用的类(Landroid/view/SearchEvent;)
08-18 04:28:02.021 31463-31463/com.示例+++++W/dalvikvm:VFY:无法解析接口方法16211:Landroid/view/Window$Callback;。onSearchRequested(Landroid/view/SearchEvent;)Z
08-18 04:28:02.021 31463-31463/com.示例+++++W/dalvikvm:VFY:无法解析接口方法16215:Landroid/view/Window$Callback;。OnWindowsStartingActionMode(Landroid/view/ActionMode$回调;I)Landroid/view/ActionMode;
08-18 04:28:02.021 31463-31463/com.示例+++++W/dalvikvm:VFY:无法解析虚拟方法532:Landroid/content/res/TypedArray;。getChangingConfigurations()I
08-18 04:28:02.021 31463-31463/com.示例+++++W/dalvikvm:VFY:无法解析虚拟方法554:Landroid/content/res/TypedArray;。getType(I)I
08-18 04:28:02.211 31463-31463/com.示例+++++W/dalvikvm:VFY:无法解析虚拟方法16639:Landroid/widget/FrameLayout;。StartActionModelForChild(Landroid/view/view;Landroid/view/ActionMode$回调;I)Landroid/view/ActionMode;
08-18 04:28:02.271 31463-31463/com.示例+++++W/dalvikvm:VFY:无法解析虚拟方法495:Landroid/content/res/Resources;。getDrawable(ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/drawable;
08-18 04:28:02.271 31463-31463/com.示例+++++W/dalvikvm:VFY:无法解析虚拟方法497:Landroid/content/res/Resources;。getDrawableForDensity(IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/drawable;
08-18 04:28:02.271 31463-31463/com.示例+++++W/dalvikvm:VFY:无法解析虚拟方法321:Landroid/content/Context;。getColorStateList(I)Landroid/content/res/ColorStateList;
08-18 04:28:02.651 31463-31463/com.example.++++++E/dalvikvm:找不到从方法android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering引用的类“android.graphics.drawable.RippleDrawable”
08-18 04:28:02.651 31463-31463/com.示例+++++W/dalvikvm:VFY:无法解析Landroid/support/v7/widget/AppCompatImageHelper中152(Landroid/graphics/drawable/RippleDrawable;)的实例;
08-18 04:35:38.731 31463-31463/com.示例+++++W/dalvikvm:VFY:无法解析虚拟方法16912:Landroid/widget/Spinner;。getPopupContext()Landroid/content/Context;
08-18 04:35:38.731 31463-31463/com.example.++++++E/dalvikvm:找不到从方法android.support.v7.widget.AppCompatSpinner$DropDownAdapter引用的类“android.widget.ThemedSpinnerAdapter”。
08-18 04:35:38.731 31463-31463/com.示例+++++W/dalvikvm:VFY:无法解析Landroid/support/v7/widget/AppCompatSpinner$DropDownAdapter中2085(Landroid/widget/ThemedSpinnerAdapter;)的实例;
08-18 04:37:23.001 31463-31463/com.示例+++++W/dalvikvm:VFY:无法解析虚拟方法196:Landroid/app/Notification$Builder;。setLocalOnly(Z)Landroid/app/Notification$Builder;
08-18 04:37:23.001 31463-31463/com.示例+++++W/dalvikvm:VFY:无法解析虚拟方法453:Landroid/content/pm/PackageManager;。getPackageInstaller()Landroid/content/pm/PackageInstaller;
08-18 04:37:32.901 31463-31463/com。示例+++++W/dalvikvm:method Landroid/support/v7/widget/ListViewCompat;。lookForSelectablePosition错误地覆盖了Landroid/widget/ListView中同名的包私有方法;
08-18 04:37:32.901 31463-31463/com.示例+++++W/dalvikvm:VFY:无法解析虚拟方法14245:Landroid/support/v7/widget/DropDownListView;。可抽出热插销(FF)V
08-18 04:37:32.901 31463-31463/com.示例+++++W/dalvikvm:VFY:无法解析虚拟方法15841:Landroid/view/view;。可抽出热插销(FF)V
08-18 04:37:39.821 31463-31463/com.示例(+++++++W/InputEventReceiver:试图完成输入事件,但输入事件接收器已被释放。

感谢您抽出时间

如果我正确理解了您的问题,您正在尝试将数据发布到PHP页面。首先,我建议使用
ASyncTask
,因为它更容易理解,并且是在Android中运行后台任务的推荐方式

要将数据发布到PHP页面,可以使用
org.apache.http.client.methods.HttpPost
org.apache.http.client.

在我的应用程序中,我是这样做的,但我不完全确定这是否是正确的做法。这段代码是通用的,只是我如何对PHP脚本执行post请求的实现

class PostRequest extends ASyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... params){
        String data = "test data";

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://url.to.post.to");

        BasicNameValuePair dataPair = new BasicNameValuePair("data", data);

        List<NameValuePair> nameValuePairList = new ArrayList<>();

        nameValuePairList.add(dataPair);

        try{

            UrlEncodedFormEntity uefe = new UrlEncodedFormEntity(nameValuePairList);
            httpPost.setEntity(uefe);

            try {
                HttpResponse httpResponse = httpClient.execute(httpPost);

                InputStream inputStream = httpResponse.getEntitiy().getContent();


                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

                StringBuilder stringBuilder = new StringBuilder();

                String bufferedStrChunk = null;

                while((bufferedStrChunk = bufferedReader.readLine()) != null){
                    stringBuilder.append(bufferedStrChunk);
                }

                return stringBuilder.toString();

            } catch(ClientProtocolException cpe){
                Log.e("app", "Exception occurred: ", cpe);
            } catch(IOException e){
                Log.e("app", "Exception occurred: ", e);
            }
        } catch(UnsupportedEncodingException e){
            Log.e("app", "Exception occurred: ", e);
        }
        return null;
    }
}
class PostRequest扩展了异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
String data=“测试数据”;
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://url.to.post.to");
BasicNameValuePair数据对=新的BasicNameValuePair(“数据”,数据);
List nameValuePairList=新的ArrayList();
nameValuePairList.add(数据对);
试一试{
UrlEncodedFormEntity uefe=新的UrlEncodedFormEntity(nameValuePairList);
httpPost.setEntity(uefe);
试一试{
HttpResponse HttpResponse=httpClient.execute(httpPost);
InputStream InputStream=httpResponse.getEntitiy().getContent(
class PostRequest extends ASyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... params){
        String data = "test data";

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://url.to.post.to");

        BasicNameValuePair dataPair = new BasicNameValuePair("data", data);

        List<NameValuePair> nameValuePairList = new ArrayList<>();

        nameValuePairList.add(dataPair);

        try{

            UrlEncodedFormEntity uefe = new UrlEncodedFormEntity(nameValuePairList);
            httpPost.setEntity(uefe);

            try {
                HttpResponse httpResponse = httpClient.execute(httpPost);

                InputStream inputStream = httpResponse.getEntitiy().getContent();


                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

                StringBuilder stringBuilder = new StringBuilder();

                String bufferedStrChunk = null;

                while((bufferedStrChunk = bufferedReader.readLine()) != null){
                    stringBuilder.append(bufferedStrChunk);
                }

                return stringBuilder.toString();

            } catch(ClientProtocolException cpe){
                Log.e("app", "Exception occurred: ", cpe);
            } catch(IOException e){
                Log.e("app", "Exception occurred: ", e);
            }
        } catch(UnsupportedEncodingException e){
            Log.e("app", "Exception occurred: ", e);
        }
        return null;
    }
}