繁体   English   中英

android用户名密码通过json发送到php - > sql

[英]android username password send through json to php-->sql

我试图找到一种方法来发布用户名和密码使用json而不是我正在使用的普通http帖子。 我正在阅读大部分教程和示例,但我却无法理解。 我有json移相器可用于从我的sql获取数据,但不是post json。

感谢您的帮助

以下是目前使用的json帖子

EditText uname = (EditText) findViewById(R.id.log_Eu_name);
            String username = uname.getText().toString();

            EditText pword = (EditText) findViewById(R.id.log_Epass);
            String password = pword.getText().toString();

            String result = new String();
            result = "";

            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs
                    .add(new BasicNameValuePair("username", username));
            nameValuePairs
                    .add(new BasicNameValuePair("password", password));

            InputStream is = null;
            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://www.loshwickphotography.com/log.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
                Log.w("SENCIDE", "Execute HTTP Post Request");

                String str = inputStreamToString(
                        response.getEntity().getContent()).toString();
                Log.w("SENCIDE", str);

                if (str.toString().equalsIgnoreCase("true")) {
                    Log.w("SENCIDE", "TRUE");
                    Toast.makeText(getApplicationContext(),
                            "FUking hell yeh!", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(),
                            "Sorry it failed", Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {

                Log.e("log_tag", "Error in http connection " + e.toString());
            }
            try {
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();

                result = sb.toString();

            } catch (Exception e) {
                Log.e("log_tag", "Error converting result " + e.toString());
            }

            try {
                if (result != null) {
                    JSONArray jArray = new JSONArray(result);
                    Log.i("log_tag", Integer.toString(jArray.length()));
                    for (int i = 0; i < jArray.length(); i++) {
                        JSONObject json_data = jArray.getJSONObject(i);

                    }
                } else {
                    Toast.makeText(getApplicationContext(), "NULL",
                            Toast.LENGTH_SHORT).show();
                }

            } catch (JSONException e) {
                Log.e("log_tag", "Error parsing data " + e.toString());

            }
        }

        private Object inputStreamToString(InputStream is) {
            // TODO Auto-generated method stub
            String line = "";
            StringBuilder total = new StringBuilder();
            // Wrap a BufferedReader around the InputStream
            BufferedReader rd = new BufferedReader(
                    new InputStreamReader(is));
            // Read response until the end
            try {
                while ((line = rd.readLine()) != null) {
                    total.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            // Return full string
            return total;
        }

    });

这是我写的这个吗?

如何写这个Php?

用这个

    JSONObject myjson=new JSONObject();
        myjson.put("userName", "someOne");
        myjson.put("password", "123");

StringEntity se = new StringEntity(myjson.toString());

httpPost.setEntity(se);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM