HttpPost响应无法读取响应

时间:2015-08-27 02:23:30

标签: java android http-post

我正在尝试将插入文本字段的数据与数据库中的数据进行比较。(LoginPage)

 package com.example.eldermonitoring;

 import java.io.InputStream;
 import java.util.ArrayList;

 import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
 import org.apache.http.NameValuePair;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.entity.UrlEncodedFormEntity;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.message.BasicNameValuePair;
 import org.apache.http.util.EntityUtils;


 import android.app.Activity;
 import android.content.Context;
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.EditText;

 public class LoginPage extends Activity {

     String un_text = null;
     String pw_text = null;

     InputStream is = null;

     String line = null;
     String result = null;
     int code;

     EditText un = null;
     EditText pw = null;

     Button submit = null;

     Context context = null;
     @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_page);
    context = this;

    un = (EditText) findViewById(R.id.txtUsername);
    pw = (EditText) findViewById(R.id.txtPassword);

    submit = (Button) findViewById(R.id.btnLogin);

    submit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            un_text = un.getText().toString();
            pw_text = pw.getText().toString();
            System.out.println("Press here");

            // tvStatus.setText("Record ");
            new insertDATA().execute("");
            System.out.println("Press here 2");


        }
    });

}

class insertDATA extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        System.out.println("1" );

     HttpClient httpclient = new DefaultHttpClient();
     HttpPost httppost = new HttpPost("http://ems1b15174.net84.net/AppLogin.php");
     httppost.setHeader("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36");
     try {
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

     nameValuePairs.add(new BasicNameValuePair("un", un_text));
     nameValuePairs.add(new BasicNameValuePair("pw", pw_text));
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

     HttpResponse response = httpclient.execute(httppost);
     HttpEntity resEntity = response.getEntity();
     if (resEntity != null) {

       String responseStr = EntityUtils.toString(resEntity).trim();
       System.out.println("Response: " +  responseStr);

// you can add an if statement here and do other actions based on the response

     if(responseStr=="caregiver")  
         System.out.println("CG");  
     else if(responseStr=="nextofkin")  
         System.out.println("NOK");  
     else   
         System.out.println("Failed");  
     }


     } catch (Exception e) {
     System.out.println("Error " + e);
     }

                return null;
            }
         }

     }

这是我收到的回复。响应是看护人,但是我无法做一个if else语句来了解角色,所以我可以将它重定向到相应的页面。 我认为我的代码在阅读响应时遇到问题,因为响应后面有一个空白区域。知道空的原因在哪里吗?

  I/System.out(1106): Press here
  I/System.out(1106): 1
  I/System.out(1106): Press here 2
  I/System.out(1106): Response: caregiver
  I/System.out(1106):       
  I/System.out(1106): <!-- Hosting24 Analytics Code -->
  I/System.out(1106): <script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
  I/System.out(1106): <!-- End Of Analytics Code -->
  I/System.out(1106): Failed

1 个答案:

答案 0 :(得分:0)

尝试responseStr.equals(&#34;看护人&#34;)