我从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来查看response.thanks提前。有什么问题请做评论。
答案 0 :(得分:0)
我希望我能正确理解你的问题,但是这段代码会以字符串形式返回响应(:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://advanmind.com/adapi/user/add/");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("email", email));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
ResponseHandler<String> res = new BasicResponseHandler();
String response = httpclient .execute(postMethod, res);
答案 1 :(得分:0)
目前还不完全清楚您的需求,但在您的代码中,您似乎想要在http://www.sencide.com/blog/login.php
网址登录。
该登录页面返回一个字符串&#34;登录失败&#34;所以你可以检查那个字符串。
执行此操作,取消标记登录网址和备注添加用户行:
HttpPost httppost = new HttpPost("http://www.sencide.com/blog/login.php")
//HttpPost httppost = new HttpPost("http://advanmind.com/adapi/user/add/");
并更改此行:
if(str.toString().equalsIgnoreCase("false") )
对此:
if(str.toString().equalsIgnoreCase("Login Failed"))