doushi1473 2017-03-21 19:17
浏览 34

使用具有不同类的Android发送JSon对象

I'm currently creating an Android app for my studies. This app have to send and receive data to and from a database.

To do so, I have a MySQL database and an Apache server. My Android app uses JSon to communicate with the server, and the server execute PHP script to communicate with database.

I'm trying to make a simple "sign in" module, but I don't know how to communicate within two classes in Android. With my code it will be more clear I think:

public class Inscription extends AppCompatActivity {
    Utilisateur u = new Utilisateur();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.inscription);

        Button connexion = (Button) findViewById(R.id.buttonInscription);
        connexion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText inputLogin = (EditText) findViewById(R.id.editTextLogin);
                EditText inputMdp = (EditText) findViewById(R.id.editText3Mdp);
                EditText inputEmail = (EditText) findViewById(R.id.editTextMail);

                Log.v("Login input: ", inputLogin.getText().toString());

                Utilisateur u = new Utilisateur();
                u.setLogin(inputLogin.getText().toString());
                u.setMdp(inputMdp.getText().toString());
                u.setMail(inputEmail.getText().toString());

                Log.v("User: ", u.toString());

                new InscriptionServeur().execute();
                Intent intent = new Intent(Inscription.this, Inscription.class);
                startActivity(intent);

            }
        });

    }


    class InscriptionServeur extends AsyncTask<String, String, String> {
        private final String url = "http://xxx.xxx.xxx.xxx/xxxx/scriptInscription.php";
        private final OkHttpClient client = new OkHttpClient();
        private Gson gson = new Gson();
        private String c;

        protected String doInBackground(String... args) {
            try {
                post(url);
            } catch (IOException e) {
                e.printStackTrace();
            }

            return "";
        }

        public String post(String url) throws IOException {
            // Prepare the request.
            c=gson.toJson(u);
           // MediaType JSON_TYPE = MediaType.parse("application/json; charset=utf-8");
            MediaType JSON_TYPE = MediaType.parse("application/json; charset=utf-8");
            Log.v("Json: ", c);
            Log.v("User bis: ", u.toString());

            Request myGetRequest = new Request.Builder()
                    .url(url)
                    .post(RequestBody.create(JSON_TYPE, c))
                    .build();
            // Get the result.
            client.newCall(myGetRequest).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Context context = getApplicationContext();

                    Toast.makeText(context, "FAIL", Toast.LENGTH_LONG);
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    Context context = getApplicationContext();

                    Toast.makeText(context, "PASS", Toast.LENGTH_LONG);
                }
            });
            return null;

        }
    }
}

So as you can see, it's quite simple. My problem is, when I log the first "User" (in the onCreate() method), the user is perfectly show, but when i do the same log with the same User in the post() method (in InscriptionServeur class), it shows nothing, and so the PHP script wont works as it doesn't accept empty form.

Could you please help me ? I'm really stuck here

thanks

  • 写回答

1条回答 默认 最新

  • dongsuiying7773 2017-03-21 19:58
    关注

    You have declared u in two places, a global one in the beginning of your code and another one inside your setOnClickListener.

    Remove this line Utilisateur u = new Utilisateur(); from your listener and it should work

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP