无法在Toast(Android)中解析并显示结果?

时间:2011-05-06 05:16:00

标签: android json parsing

我是Android的新手,所以我在HTTP请求的JSON解析中遇到问题。让我简要介绍一下。实际上我正在尝试登录,用户将在其中键入“email”作为用户名,并输入“password”作为密码,只要他点击登录按钮,就会在URL http://excoflare.com/dev2010/bb_snet/baranzan/index.php?json=json&UM_email="+sUserName+"&UM_password="+sPassword上发出http请求,如果成功的 Toast 消息将在同一布局上到达“欢迎用户名”。我有两个类(i)HelloAndroid.java和(ii)RestJsonClient.java

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();
    }

}

现在我正在尝试只有正确/有效的电子邮件和密码,现在只有我想要的。 请帮忙。    感谢很多

1 个答案:

答案 0 :(得分:0)

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

在这里你应该有一个if-else条件来检查你从json响应得到的数据。在你的情况下,你应该检查用户身份验证。如果用户进行身份验证,那么您应该显示Toast else。