使用齐射将数据从dialogfragment发送到mysql dataabse

时间:2017-05-16 11:54:12

标签: php android mysql

我有一个对话片段的片段

在对话框片段中有一个编辑文本和按钮

我想在推送按钮“发送”时从dialogfragment的Edittext向mysql数据库发送信息 任何人都可以帮我这个片段的代码 m php文件工作正常,我的服务器也用登录和注册来测试它

我认为我的应用程序不会将请求发送到服务器。

package com.kid.crypto.cryptokid;


import android.app.Dialog;
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.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;

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

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


/**
 * A simple {@link Fragment} subclass.
 */
public class Sport extends Fragment implements View.OnClickListener {
    String URLup="http://v3rlust-001-site1.etempurl.com/up.php" ;


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


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


        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_sport, container, false);
        Button task1 = (Button) view.findViewById(R.id.task1b);
        task1.setOnClickListener(this);
        return view;
    }

    public void dialogTask1() {
        final Dialog task1 = new Dialog(getActivity());
        task1.setTitle("TASK 1 20 point");
        task1.setContentView(R.layout.task1sport);
        Button a = (Button) task1.findViewById(R.id.task1submit);

        a.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText answer = (EditText) task1.findViewById(R.id.answertask1);
                String answer1 = answer.getText().toString();
                if (answer1.contains("tunis")) {
                    Bundle bundle = getActivity().getIntent().getExtras();
                    int Score=100
                    final String Stscore1 = Integer.toString(Score) ;
                    StringRequest stringRequest = new StringRequest(Request.Method.POST,URLup,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {

                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }){
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map <String,String> params= new HashMap<String, String>() ;

                            params.put("score",Stscore1) ;

                            return params ;
                        }
                    }; Volleyclass.getmInstance(getActivity()).addToRequestque(stringRequest);


                } else {
                    Toast msg1 = Toast.makeText(getActivity(), "false answer", Toast.LENGTH_LONG);
                    msg1.show();

                }

            }
        });

                 task1.show() ;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.task1b:
                dialogTask1();
                break;

        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试调试。无需调试如何找到任何错误。当volley没有返回成功响应时,你可以在这里查看错误。

private void callLoginService() {
        try {
            if (globalInstance.isNetworkAvailable(MainActivity.this)) {
                String logindataxml = loginXML();
                if (logindataxml.length() < 0) {
                    return;
                } else {
                    RequestQueue queue = Volley.newRequestQueue(this);
                    JSONObject obj = new JSONObject();
                    obj.put("xmlData", logindataxml);
                    JsonObjectRequest postRequest = new JsonObjectRequest(Constants.url.login, obj,
                            new Response.Listener<JSONObject>() {
                                @Override
                                public void onResponse(JSONObject response) {

                                   } catch (JSONException e) {
                                        e.printStackTrace();
                                    }

                                }
                            },
                            new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {
                                    String msg = error.getMessage();
                                    Log.d("Error.Response", "");
                                }
                            }
                    );

                  queue.add(postRequest);
                }
            }
        } catch (Exception e) {
            globalInstance.writeLogs(this, new Object() {
            }.getClass().getEnclosingMethod().getName(), true, e.toString(), Constants.LogType.ERROR_LOG);

        }
    }