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;
}
文字に解析するための詳細コードを教えてください。トーストを使ってレスポンスを表示しています。よろしくお願いします。問題があればコメントしてください。