Java 添加不在Android碎片上工作的产品(记录)
我需要一些帮助关于我的添加功能在Android片段。我不知道我哪里做错了。。。很抱歉我对Java的了解有限 当我单击submit按钮时,什么也没有发生……我认为从onResponse方法开始出现了一些错误 这些函数假设将我有关键字的记录(其中包括名称、价格和描述)添加到数据库中,如果记录成功添加,它将提醒用户“产品成功添加”,否则它将通知用户添加记录失败 代码在“活动”页面上运行良好(我从“注册帐户”活动中复制代码),但在将其移动到“片段”页面时不起作用 这是AddFragment.javaJava 添加不在Android碎片上工作的产品(记录),java,android,Java,Android,我需要一些帮助关于我的添加功能在Android片段。我不知道我哪里做错了。。。很抱歉我对Java的了解有限 当我单击submit按钮时,什么也没有发生……我认为从onResponse方法开始出现了一些错误 这些函数假设将我有关键字的记录(其中包括名称、价格和描述)添加到数据库中,如果记录成功添加,它将提醒用户“产品成功添加”,否则它将通知用户添加记录失败 代码在“活动”页面上运行良好(我从“注册帐户”活动中复制代码),但在将其移动到“片段”页面时不起作用 这是AddFragment.java p
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;
}
}
包ezms.ezms;
导入android.app.AlertDialog;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.Toast;
导入com.android.volley.RequestQueue;
导入com.android.volley.Response;
导入com.android.volley.toolbox.volley;
导入org.json.JSONException;
导入org.json.JSONObject;
/**
*一个简单的{@link Fragment}子类。
*/
公共类AddFragment扩展了Fragment{
公共AddFragment(){
//必需的空公共构造函数
}
编辑文本pName、pPrice、pDesc;
按钮btn_添加产品;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment\u 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=(按钮)view.findviewbyd(R.id.btn_addProduct);
btn_addProduct.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
最终字符串名称=pName.getText().toString();
最终字符串价格=pPrice.getText().toString();
最终字符串desc=pDesc.getText().toString();
Response.Listener responseListener=新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
试一试{
JSONObject jsonResponse=新的JSONObject(响应);
boolean success=jsonResponse.getBoolean(“success”);
如果(成功){
AlertDialog.Builder myAlertDialog=新建AlertDialog.Builder(AddFragment.this.getActivity());
myAlertDialog.setMessage(“已成功添加新产品”);
myAlertDialog.setPositiveButton(“明白了”,新的DialogInterface.OnClickListener(){
公共void onClick(对话框接口arg0,int arg1){
Toast.makeText(AddFragment.this.getActivity(),“新产品已成功添加。”,Toast.LENGTH_SHORT).show();
}
});
myAlertDialog.show();
}否则{
AlertDialog.Builder=新建AlertDialog.Builder(AddFragment.this.getActivity());
builder.setMessage(“添加产品失败”)
.setNegativeButton(“重试”,null)
.create()
.show();
}
}捕获(JSONException e){
e、 printStackTrace();
}
}
};
AddProductRequest AddProductRequest=新的AddProductRequest(名称、价格、描述、响应侦听器);
RequestQueue=Volley.newRequestQueue(AddFragment.this.getActivity());
添加(AddProductRequest);
}
});
返回视图;
}
}
这是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;
}
}
包ezms.ezms;
导入com.android.volley.Response;
导入com.android.volley.toolbox.StringRequest;
导入java.util.HashMap;
导入java.util.Map;
公共类AddProductRequest扩展了StringRequest{
私有静态最终字符串ADD\u REQUEST\u URL=”http://www.pohqw.com/projects/myapp/add_product.php";
私有映射参数;
//这个类的构造函数
public AddProductRequest(字符串名称、字符串价格、字符串描述、响应.侦听器){
super(Method.POST,ADD\u REQUEST\u URL,listener,null);
params=新的HashMap();
参数put(“名称”,名称);
参数卖出价(“价格”,价格);
参数put(“描述”,描述);
}
@凌驾
公共映射getParams(){
返回参数;
}
}
这是在我的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)
05-09 13:12:47.004 10674-12581/?I/System.out:[CDS]接收超时:1
05-09 13:12:47.004 10674-12581/? I/System.out:[CDS]接收超时:2500
05-09 13:12:47.004 10674-12581/? I/System.out:在OkHttp:0中关闭
05-09 13:12:47.004 10674-12581/? I/System.out:[CDS]关闭[38156]
05-09 13:12:47.004 10674-12581/? D/libc netbsd:[getaddrinfo]:主机名=www.pohqw.com;servname=(null);cache_mode=(null),netid=0;马克=0
05-09 13:12:47.005 10674-12581/? D/libc netbsd:[getaddrinfo]:ai_addrlen=0;ai_canonname=(空);爱_旗=4;艾尤族=0
05-09 13:12:47.005 10674-12581/? D/libc netbsd:[getaddrinfo]:主机名=www.pohqw.com;servname=(null);cache_mode=(null),netid=0;马克=0
05-09 13:12:47.005 10674-12581/? D/libc netbsd:[getaddrin
RequestQueue queue = Volley.newRequestQueue(AddFragment.this.getActivity());
RequestQueue queue = Volley.newRequestQueue(getActivity().getApplicationContext());