简体   繁体   English

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

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

im trying to find a way to post username and password using json rather than normal http post that im currently using. 我试图找到一种方法来发布用户名和密码使用json而不是我正在使用的普通http帖子。 i have being going through most of the tutorials and examples to undestand but yet i was unable to get an idea. 我正在阅读大部分教程和示例,但我却无法理解。 i have json phasers available to get the data from my sql but not the post json. 我有json移相器可用于从我的sql获取数据,但不是post json。

thank you for the help 感谢您的帮助

following is the currently used json post 以下是目前使用的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;
        }

    });

Is this correct which i have written? 这是我写的这个吗?

how to write the Php for this? 如何写这个Php?

use this 用这个

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

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

and httpPost.setEntity(se); httpPost.setEntity(se);

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

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