我从 http post(一个 php 服务器)得到我的响应但是,我想将它解析为简单的字符。怎么做 。
这是我用于 HttpPost 的以下方法
public void postLoginData()
{
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
/* login.php returns true if username and password is equal to saranga */
// HttpPost httppost = new HttpPost("http://www.sencide.com/blog/login.php");
HttpPost httppost = new HttpPost("http://advanmind.com/adapi/user/add/");//username and password is xyz
try {
// Add user name and password
EditText usermail = (EditText)findViewById(R.id.editText1);
String email = usermail.getText().toString();
EditText pword = (EditText)findViewById(R.id.editText2);
String password = pword.getText().toString();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("email", email));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
Log.w("ADPORTAL", "Execute HTTP Post Request");
HttpResponse response = httpclient.execute(httppost);
String str = inputStreamToString(response.getEntity().getContent()).toString();
Log.w("ADPORTAL", str);
if(str.toString().equalsIgnoreCase("false") )
{
Log.w("ADPORTAL", "FALSE");
Toast.makeText(getBaseContext(), str, 10000).show();
result.setText("You are not registerd please register");
//Intent AfterLogin = new Intent(this, AfterLogin.class);
//startActivity(AfterLogin);
}else
{Log.w("SENCIDE", "TRUE");
// Intent AfterLogin = new Intent(this,AfterLogin.class);
// startActivity(AfterLogin);
Toast.makeText(getBaseContext(), str, 10000).show();
//result.setText(str+" loginsuccess");
// Intent registration = new Intent(this, Registration.class);
//startActivity(registration);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
输入流方法
private StringBuilder inputStreamToString(InputStream is) {
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;
}
请告诉我将其解析为字符的详细代码。我正在使用 toast 来查看响应。在此先感谢。任何问题请发表评论。