Unable to parse and display result in Toast (Android)?
I am newbie in Android, so I am getting problem in JSON parsing on HTTP Request. Let me give a brief about that. Actually I am trying to make a login, from where a user will type "email" as username and "password" as passsword, as soon as he clicks on login button a http request will be made on url http://excoflare.com/dev2010/bb_snet/baranzan/index.php?json=json&UM_email="+sUserName+"&UM_password="+sPassword
and if succeded a Toast message will arrive "Welcome username" on the same layout. I have two class (i) HelloAndroid.java and (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();
开发者_StackOverflow }
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();
}
}
Now i am trying with only correct/valid email and password, for now that only i want. PLEASE HELP. THANKS A LOT
JSONObject json = RestJsonClient.connect(address);
/* is something missing here if yes Please HELP*/
next();
Here you should have a if-else condition to check the data that you are getting from the json response. in you case you should check the user authentication . The if the user is authenticate then you should show the Toast else not.
精彩评论