添加产品(记录)不适用于Android片段

时间:2016-05-09 04:41:08

标签: java android

我需要一些关于Android Fragment中的添加功能的帮助。我不知道我做错了什么...很抱歉我对Java的知识有限......

当我点击提交按钮时,没有发生任何事情...我认为从onResponse方法开始出错了。

这些功能假设添加我有键入的记录,名称,价格和&对数据库的描述,如果记录成功添加,它将提醒用户"产品成功添加"否则会通知用户无法添加记录。

代码在活动页面上运行良好(我从我的注册帐户活动中复制代码),但在将其移动到片段页面时无效。

这是AddFragment.java

package ezms.ezms;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * A simple {@link Fragment} subclass.
 */
public class AddFragment extends Fragment {


    public AddFragment() {
        // Required empty public constructor
    }

    EditText pName, pPrice, pDesc;
    Button btn_addProduct;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_add, container, false);

        pName = (EditText) view.findViewById(R.id.pName);
        pPrice = (EditText) view.findViewById(R.id.pPrice);
        pDesc = (EditText) view.findViewById(R.id.pDesc);
        btn_addProduct = (Button) view.findViewById(R.id.btn_addProduct);

        btn_addProduct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final String name = pName.getText().toString();
                final String price = pPrice.getText().toString();
                final String desc = pDesc.getText().toString();

                Response.Listener<String> responseListener = new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonResponse = new JSONObject(response);
                            boolean success = jsonResponse.getBoolean("success");
                            if (success) {
                                AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(AddFragment.this.getActivity());
                                myAlertDialog.setMessage("New Product Successfully Added.");
                                myAlertDialog.setPositiveButton("GOT IT", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface arg0, int arg1) {
                                            Toast.makeText(AddFragment.this.getActivity(), "New Product Successfully Added.", Toast.LENGTH_SHORT).show();
                                        }
                                });
                                myAlertDialog.show();

                            } else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(AddFragment.this.getActivity());
                                builder.setMessage("Failed to Add Product.")
                                        .setNegativeButton("RETRY", null)
                                        .create()
                                        .show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                };

                AddProductRequest AddProductRequest = new AddProductRequest(name, price, desc, responseListener);
                RequestQueue queue = Volley.newRequestQueue(AddFragment.this.getActivity());
                queue.add(AddProductRequest);
            }
        });

        return view;
    }
}

这是AddProductRequest.java

package ezms.ezms;

import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

public class AddProductRequest extends StringRequest {
    private static final String ADD_REQUEST_URL = "http://www.pohqw.com/projects/myapp/add_product.php";
    private Map<String, String> params;

    // Constructor of this class
    public AddProductRequest(String name, String price, String desc, Response.Listener<String> listener) {
        super(Method.POST, ADD_REQUEST_URL, listener, null);
        params = new HashMap<>();
        params.put("name", name);
        params.put("price", price);
        params.put("desc", desc);
    }

    @Override
    public Map<String, String> getParams() {
        return params;
    }
}

这是我在Android Studio上显示的错误消息..

05-09 13:12:47.004 10674-12581/? I/System.out: [CDS]rx timeout:1
05-09 13:12:47.004 10674-12581/? I/System.out: [CDS]rx timeout:2500
05-09 13:12:47.004 10674-12581/? I/System.out: Close in OkHttp:0
05-09 13:12:47.004 10674-12581/? I/System.out: [CDS]close[38156]
05-09 13:12:47.004 10674-12581/? D/libc-netbsd: [getaddrinfo]: hostname=www.pohqw.com; servname=(null); cache_mode=(null), netid=0; mark=0
05-09 13:12:47.005 10674-12581/? D/libc-netbsd: [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
05-09 13:12:47.005 10674-12581/? D/libc-netbsd: [getaddrinfo]: hostname=www.pohqw.com; servname=(null); cache_mode=(null), netid=0; mark=0
05-09 13:12:47.005 10674-12581/? D/libc-netbsd: [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=1024; ai_family=0
05-09 13:12:47.007 10674-12581/? D/libc-netbsd: getaddrinfo: www.pohqw.com get result from proxy >>
05-09 13:12:47.007 10674-12581/? I/System.out: propertyValue:true
05-09 13:12:47.008 10674-12581/? I/System.out: [CDS]rx timeout:2500
05-09 13:12:47.009 10674-12581/? I/System.out: [socket][5] connection www.pohqw.com/202.75.56.237:80;LocalPort=58216(2500)
05-09 13:12:47.009 10674-12581/? I/System.out: [CDS]connect[www.pohqw.com/202.75.56.237:80] tm:2
05-09 13:12:47.010 10674-12581/? D/Posix: [Posix_connect Debug]Process ezms.ezms :80 
05-09 13:12:47.066 10674-12581/? I/System.out: [socket][/192.168.1.34:58216] connected
05-09 13:12:47.067 10674-12581/? I/System.out: [OkHttp] sendRequest>>
05-09 13:12:47.067 10674-12581/? I/System.out: [OkHttp] sendRequest<<
05-09 13:12:47.385 10674-10674/? W/System.err: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
05-09 13:12:47.385 10674-10674/? W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
05-09 13:12:47.385 10674-10674/? W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:160)
05-09 13:12:47.385 10674-10674/? W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:173)
05-09 13:12:47.385 10674-10674/? W/System.err:     at ezms.ezms.AddFragment$1$1.onResponse(AddFragment.java:59)
05-09 13:12:47.385 10674-10674/? W/System.err:     at ezms.ezms.AddFragment$1$1.onResponse(AddFragment.java:55)
05-09 13:12:47.385 10674-10674/? W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
05-09 13:12:47.385 10674-10674/? W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
05-09 13:12:47.385 10674-10674/? W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
05-09 13:12:47.385 10674-10674/? W/System.err:     at android.os.Handler.handleCallback(Handler.java:815)
05-09 13:12:47.385 10674-10674/? W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:104)
05-09 13:12:47.385 10674-10674/? W/System.err:     at android.os.Looper.loop(Looper.java:194)
05-09 13:12:47.385 10674-10674/? W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5546)
05-09 13:12:47.385 10674-10674/? W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
05-09 13:12:47.385 10674-10674/? W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
05-09 13:12:47.385 10674-10674/? W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
05-09 13:12:47.385 10674-10674/? W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)

2 个答案:

答案 0 :(得分:0)

显然,当我尝试你的URL POST时,我得到了这个:

  

警告:mysqli_stmt_bind_param()要求参数1为mysqli_stmt,第9行/home/pohqw/public_html/projects/myapp/add_product.php中给出布尔值

     

警告:mysqli_stmt_execute()要求参数1为mysqli_stmt,第10行/home/pohqw/public_html/projects/myapp/add_product.php中给出布尔值   {&#34;成功&#34;:真}

似乎您的警告会与您的{"success":true}

一起被抛出

答案 1 :(得分:-1)

尝试替换

    RequestQueue queue = Volley.newRequestQueue(AddFragment.this.getActivity());

RequestQueue queue = Volley.newRequestQueue(getActivity().getApplicationContext());