2

我正在研究通过 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;
}

正如您可以从大量可笑的 Log 语句中看出的那样,我一直在调试但没有成功。我的 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":"test@testing11.com","password":"oflndveledmenkddjevhhlkh","email":"test@testing.com","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_ACCOUNT检查侦听器的类名时,它都不相等。但是当我检查输出时,它们看起来都和我一样。我没有将任何内容与 进行比较==,所有内容都使用.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);
}
}

}
4

1 回答 1

3

回调名称是类 com.example.appname.EditAccountActivity

EDIT_ACCOUNT 常量是com.example.appname.EditAccountActivity

要解决此问题,请使用 callbackListener.getClass().getName(),而不是使用 callbackListener.getClass().toString()。——歌舞伎

于 2013-07-10T18:33:45.823 回答