2015-08-27 3 views
0

Я пытаюсь сравнить свои данные, вставленные в текстовое поле с данными в базе данных. (LoginPage)HttpPost ответ не может прочитать ответ

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 
+0

@ John3136 я не вижу сходство – Agnes

+0

'если (responseStr == "Опекун")' не выглядит как 'если (responseStr.equals («caregiver») ' – John3136

+0

@ John3136 Я пробовал его перед публикацией, но он все еще не работает – Agnes

ответ

Смежные вопросы