我正在处理应用程序的一部分,它通过http post在服务器上创建用户对象。在将回调类的名称与字符串常量匹配时,我遇到了一个非常令人沮丧的问题。
上下文:
由于这个应用程序发出大约8-10个不同的服务器请求,我尝试将实际的连接过程封装到一个类中,HTTPConnection。每个需要连接到服务器的活动都会传递自己的AndroidHttpClientHttpRequestHTTPConnectionListener实例,这是每个调用活动都实现的接口,允许将服务器响应直接发送回活动。
这一直工作得很好,直到我不得不实现应用程序的一部分,它需要返回一个响应头。在这之前,我只需要处理响应体,所以HTTPConnection只返回了响应体。为了解决这个问题,我重写了HTTPConnection来检查回调类的名称,然后根据请求数据的类返回头或正文。
看似简单的解决办法现在已经占用了我一大早的时间,所以我意识到我需要更多的眼睛来看到我做错了什么。
问题是:
HTTPConnection的构造函数中,我存储callbacklistener(实际上是实现HTTPConnectionListener的任何东西)。在本例中,我的调用活动EditAccountActivity是回调。我在doinbackground()的早期将其保存在一个字段中,以便稍后进行检查。

    String callbackName = callbackListener.getClass().toString(); // Get the full name of the callback class and store it here.
    Log.v(TAG, "** Callback listener is set to " + callbackName);

然后,在完成连接之后,我想检查一个常量(最终是一个常量列表),以确定是否返回响应的主体或头。
// The constant I am trying to match
final protected String EDIT_ACCOUNT = "com.example.appname.EditAccountActivity";

返回主体或响应的逻辑如下。
// Determine which section of response to return
if (callbackName.equals(EDIT_ACCOUNT)) {
    Header responseHeader = response.getFirstHeader("Location");
    return responseHeader.getValue();
} else {
return serverResponse;
}

从大量的日志语句可以看出,我一直在调试,但没有成功。我的logcat输出如下。
07-10 12:44:54.932: INFO/com.example.appname.ServerFetcher(21650): Entity set: HTTP.CONTENT_TYPE - application/json
07-10 12:44:54.932: VERBOSE/com.example.appname.ServerFetcher(21650): Sending user json:     {"last_name":"tester ","first_name":"tester       ","username":"[email protected]","password":"oflndveledmenkddjevhhlkh","email":"[email protected]","profession":"tester "}
07-10 12:44:54.952: VERBOSE/com.example.appname.HTTPConnection(21650): ** Callback listener is set to class com.example.appname.EditAccountActivity
07-10 12:44:54.952: INFO/com.example.appname.HTTPConnection(21650): Executing the HTTP POST request to <url>
07-10 12:44:55.713: INFO/com.example.appname.HTTPConnection(21650): ** HTTP Response returned: 201 - CREATED
07-10 12:44:55.713: INFO/com.example.appname.HTTPConnection(21650): ** 201 - CREATED
07-10 12:44:55.713: INFO/com.example.appname.HTTPConnection(21650): Response body:
07-10 12:44:55.713: VERBOSE/com.example.appname.HTTPConnection(21650): Callback name is class com.example.appname.EditAccountActivity
07-10 12:44:55.713: VERBOSE/com.example.appname.HTTPConnection(21650): Listener is class com.example.appname.EditAccountActivity
07-10 12:44:55.713: VERBOSE/com.example.appname.HTTPConnection(21650): Are they equal? true
07-10 12:44:55.713: VERBOSE/com.example.appname.HTTPConnection(21650): EDIT_ACCOUNT constant is com.example.appname.EditAccountActivity
07-10 12:44:55.713: VERBOSE/com.example.appname.HTTPConnection(21650): Is callbackName equal to EDIT_ACCOUNT? false
07-10 12:44:55.713: VERBOSE/com.example.appname.HTTPConnection(21650): Is callbackListener.getClass().toString() equal to EDIT_ACCOUNT? false
07-10 12:44:55.723: VERBOSE/com.example.appname.EditAccountActivity(21650): onConnectionFinish called.
07-10 12:44:55.743: VERBOSE/com.example.appname.EditAccountActivity(21650): Response returned from user creation is

出于某种原因,每当我用edit_帐户检查侦听器的类名时,它就不相等。但当我检查输出时,它们看起来都和我一样。我没有将任何内容与==进行比较,而是使用.equals()检查所有内容。因此,if语句的主体永远不会执行,这意味着我输出一个空字符串(因为在这个特定的连接中没有响应主体)。
任何关于如何进行的建议都会帮助我保持头脑清醒。如果需要的话,这是全班同学。
httpconnection.java
package com.example.appname;

public class HTTPConnection extends AsyncTask<Void, Integer, String> {

final protected String TAG = HTTPConnection.class.getName();
protected AndroidHttpClient httpClient;
protected HttpRequestBase httpRequest;
protected HTTPConnectionListener callbackListener;

// Callback Class Name Constants
final protected String EDIT_ACCOUNT = "com.example.appname.EditAccountActivity";

public HTTPConnection(AndroidHttpClient httpClient, HttpRequestBase httpRequest, HTTPConnectionListener listener) {
    this.httpClient = httpClient;
    this.httpRequest = httpRequest;
    this.callbackListener = listener;
}

@Override
protected String doInBackground(Void.. voids) {
    String serverResponse = null;
    String callbackName = callbackListener.getClass().toString(); // Get the full name of the callback class and store it here.
    Log.v(TAG, "** Callback listener is set to " + callbackName);

    try {
        Log.i(TAG, "Executing the HTTP " + httpRequest.getMethod() + " request to " + httpRequest.getURI().toString());
        HttpResponse response;
        response = httpClient.execute(httpRequest);

        // Grab the returned string as it is returned and make it a String to save memory.
        StringBuilder stringBuilderResponse = inputStreamToString(response.getEntity().getContent());
        serverResponse = stringBuilderResponse.toString();

        // Log the response.
        Log.i(TAG, "** HTTP Response returned: " + response.getStatusLine().getStatusCode() + " - " + response.getStatusLine().getReasonPhrase());
        Log.i(TAG, "** Response Code: " + response.getStatusLine().getStatusCode() + " Reason Code: " + response.getStatusLine().getReasonPhrase());
        Log.i(TAG, "Response body: " + serverResponse);

        // ** Debug string equality **
        Log.v(TAG, "Callback name is " + callbackName);
        Log.v(TAG, "Listener is " + callbackListener.getClass().toString());
        boolean equality1 = callbackName.equals(callbackListener.getClass().toString());
        Log.v(TAG, "Are they equal? " + equality1);
        Log.v(TAG, "EDIT_ACCOUNT constant is " + EDIT_ACCOUNT);
        boolean equality2 = callbackName.equals(EDIT_ACCOUNT);
        Log.v(TAG, "Is callbackName equal to EDIT_ACCOUNT? " + equality2);
        boolean equality3 = callbackListener.getClass().toString().equals(EDIT_ACCOUNT);
        Log.v(TAG, "Is callbackListener.getClass().toString() equal to EDIT_ACCOUNT? " + equality3);

        // Determine which section of response to return
        if (callbackName.equals(EDIT_ACCOUNT)) {
            Header responseHeader = response.getFirstHeader("Location");
            return responseHeader.getValue();
        } else {
        return serverResponse;
        }

    } catch (IOException e) {
        Log.e(TAG, "Error when executing HTTP " + httpRequest.getMethod() + " request!");
        e.printStackTrace();
        return serverResponse;
    }
}

@Override
protected void onPostExecute(String result) {
    httpClient.close();
    callbackListener.onConnectionFinish(result);
}
}

}

最佳答案

回调名称是类com.example.appname.editAccountActivity
编辑帐户常数是com.example.appname.editaccountactivity
要解决此问题,请使用callbacklistener.getClass().toString()而不是callbacklistener.getClass().getName()。–歌舞伎

07-27 20:48