ds0678 2012-07-12 06:55
浏览 17
已采纳

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

im trying to find a way to post username and password using json rather than normal http post that im currently using. 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.

thank you for the help

following is the currently used json post

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 + "
");
                }
                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?

  • 写回答

1条回答 默认 最新

  • dsjq6977 2012-07-12 07:05
    关注

    use this

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

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

    and httpPost.setEntity(se);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)