Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从服务器(Android)获取响应文本_Android_Response - Fatal编程技术网

从服务器(Android)获取响应文本

从服务器(Android)获取响应文本,android,response,Android,Response,我通过Asynctask中的以下代码获得服务器响应 try { // Add your data List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("email", emailText)); nameVal

我通过Asynctask中的以下代码获得服务器响应

 try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("email", emailText));
            nameValuePairs.add(new BasicNameValuePair("password", passwordText));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            Log.e("post_", nameValuePairs.toString());
            // Execute HTTP Post Request
            response = httpclient.execute(httppost);
            serverResponse=EntityUtils.toString(response.getEntity());
            Log.e("RESPONSE1",serverResponse);
            return serverResponse;
        } 

如何在按钮单击中获取响应的正确值?

将此代码添加到
onPostExecute()
方法的
loginData
类中

Log.e("SERVERRESPONSE1",serverResponse);
if(serverResponse.equals("Success") ) {
    Intent i= new Intent(getApplicationContext(),IndexActivity.class);
    startActivity(i);
} else if (serverResponse.equals("Fail")){
    Toast.makeText(getBaseContext(),"Invalid Username/Password!!",Toast.LENGTH_LONG).show();
} else {
    Toast.makeText(getBaseContext(),"Invalid Username/Password!!",Toast.LENGTH_LONG).show();
}
并将其从
onClick()
方法中删除

您应该在从asynctask获得响应后通知用户。
浏览此链接以了解。

您将收到一个空错误,因为您没有在检查服务器响应之前给异步任务完成时间。适当的方法是在异步任务的
onPostExecute
部分中处理该逻辑。像这样的

public class GetServerResponse extends AsyncTask<Void, Void, String> {
 String emailText, passwordText;


public GetDetailsAsyncTask(String emailText, String passwordText) {
     this.emailText=emailText;
     this.passwordText = passwordText;
}



@Override
protected String doInBackground(Void... params) {

    if (isCancelled())
        return null;
    return [Call Method that accepts emailText and passwordText && returns a string (response text)];
}

@Override
protected void onPostExecute(String serverResponse) {
    if(serverResponse!=null){
        if (serverResponse =="Success" ) {
            Intent i= new Intent(getApplicationContext(),IndexActivity.class);
            startActivity(i);
        }
        else if (serverResponse=="Fail"){
            Toast.makeText(getBaseContext(),"Invalid Username/Password!!",Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText(getBaseContext(),"Invalid Username/Password!!",Toast.LENGTH_LONG).show();
        }
    }
}

    @Override
    protected void onPreExecute() {
    }

}
然后在包装活动/课程中,您可以执行以下操作

  GetServerResponse oReturn = new GetServerResponse (email,password);
    oReturn.setListener(new onServerResponselistener () {

        @Override
        public void success() {
        //handle a successful call
        //go to the next activity
        }

        @Override
        public void fail() {
        //popup "incorrect credentials etc."
        }
    });
oReturn.execute();

请添加详细代码并粘贴logcat输出。我现在已编辑。请检查,但现在我无法导航到其他活动。(我收到成功消息)@AmitNair-您可以实现一个界面,其中包装活动可以覆盖。我将更新代码非常感谢您的帮助..:),但现在我也无法导航到其他活动。(我收到成功消息)尝试用您的活动替换getApplicationContext()。这可能会对您有所帮助。@Divya非常感谢您的帮助..:)
//inside the async task class
public void setListener(onServerResponselistener listener) {
    this.mListener = listener;
}

public interface onServerResponselistener{
    public void success();

    public void fail();
}

//then in your post execute, you could call this.mListener.success()/fail() depending on the response. Make sure to check for null.
  GetServerResponse oReturn = new GetServerResponse (email,password);
    oReturn.setListener(new onServerResponselistener () {

        @Override
        public void success() {
        //handle a successful call
        //go to the next activity
        }

        @Override
        public void fail() {
        //popup "incorrect credentials etc."
        }
    });
oReturn.execute();