2011-05-06 46 views
0

我是Android新手,所以我在JSON解析HTTP請求時遇到問題。讓我簡單介紹一下。其實我試圖做一個登錄,從用戶將輸入「電子郵件」作爲用戶名和「密碼」作爲passsword,只要他點擊登錄按鈕一個http請求將在url http://excoflare.com/dev2010/bb_snet/baranzan/index.php?json=json&UM_email="+sUserName+"&UM_password="+sPassword,如果成功了吐司消息將在同一個佈局上到達「歡迎用戶名」。我有兩個(i)類HelloAndroid.java及(ii)RestJsonClient.java無法解析並在Toast(Android)中顯示結果?

HelloAndroid.java

public class HelloAndroid extends Activity implements OnClickListener { 
        /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle icicle) { 
         super.onCreate(icicle); 
         setContentView(R.layout.main); 
         Button login = (Button)findViewById(R.id.login_button); 
         login.setOnClickListener(this); 
        } 

        public void onClick(View v) { 
         EditText usernameEditText = (EditText) findViewById(R.id.txt_username); 
         EditText passwordEditText = (EditText) findViewById(R.id.txt_password); 
         String sUserName = usernameEditText.getText().toString(); 
         String sPassword = passwordEditText.getText().toString(); 
         String address = "http://excoflare.com/dev2010/bb_snet/baranzan/index.php?json=json&UM_email="+sUserName+"&UM_password="+sPassword+""; 
         JSONObject json = RestJsonClient.connect(address); 
          /* is something missing here if yes Please HELP*/ 
         next(); 
        } 

        private void next(){ 
         Toast.makeText(HelloAndroid.this, "Welcome username", Toast.LENGTH_SHORT).show(); 
        } 

        } 

RestJsonClient

public class RestJsonClient { 

    public static JSONObject connect(String url) 
    { 

     HttpClient httpclient = new DefaultHttpClient(); 

     // Prepare a request object 
     HttpGet httpget = new HttpGet(url); 

     // Execute the request 
     HttpResponse response; 

     JSONObject json = new JSONObject(); 

     try { 
      response = httpclient.execute(httpget); 

      HttpEntity entity = response.getEntity(); 

      if (entity != null) { 

       // A Simple JSON Response Read 
       InputStream instream = entity.getContent(); 
       String result= convertStreamToString(instream); 

       json=new JSONObject(result); 

       instream.close(); 
      } 

     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     return json; 
    } 
    /** 
    * 
    * @param is 
    * @return String 
    */ 
    public static String convertStreamToString(InputStream is) { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
     StringBuilder sb = new StringBuilder(); 

     String line = null; 
     try { 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       is.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return sb.toString(); 
    } 

} 

現在我只用正確的努力/有效的電子郵件和密碼,現在只有我想要。 請幫助。 非常感謝

+0

檢查你是否在logcat中獲得任何exeception .... – Dinash 2011-05-06 05:21:32

+0

convertStreamToString方法返回什麼? – 2011-05-06 06:50:12

回答

0
JSONObject json = RestJsonClient.connect(address); 
         /* is something missing here if yes Please HELP*/ 
        next(); 

在這裏,你應該有一個的if-else條件檢查你是從JSON響應獲取數據。在你的情況下,你應該檢查用戶認證。如果用戶認證,那麼你應該顯示其他的不是。