2012-07-12 53 views
0

即時通訊嘗試找到一種方式來發布用戶名和密碼使用JSON,而不是正常的http後,即時通訊使用。我正在瀏覽大部分的教程和例子,但我仍然無法得到一個想法。我有json phasers可以從我的sql中獲取數據,但不是post json。Android的用戶名密碼通過JSON發送到PHP - > sql

感謝你的幫助

以下是目前使用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?

回答

2

使用本

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

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

httpPost.setEntity(se);

+0

嘿日Thnx男子....但可以ü請查看因爲它的工作不適合我上面:( – Loshi 2012-07-12 09:50:08

相關問題