我正在处理通过HTTP POST在服务器上创建用户对象的应用程序的一部分。将回调类的名称与字符串常量相匹配,我遇到了一个非常令人沮丧的问题。
由于此应用程序发出大约8-10个不同的服务器请求,因此我尝试将实际的连接过程封装到一个类HTTPConnection
中。每个需要连接到服务器的活动都会传递自己的AndroidHttpClient
,HttpRequest
和HTTPConnectionListener
实例,这是每个调用活动实现的接口,允许发送服务器响应直接回到活动。
这一直很有效,直到我必须实现需要回复标题的应用程序的一部分。在此之前,我只需处理响应 body ,因此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语句的主体永远不会被执行,这意味着我输出一个空字符串(如在这个特定的连接中没有响应体)。
任何有关如何进行的建议都有助于保持头发。如果需要,这是全班。
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);
}
}
}
答案 0 :(得分:3)
回调名称为 类com.example.appname.EditAccountActivity
EDIT_ACCOUNT常量为 com.example.appname.EditAccountActivity
要解决此问题,请使用callbackListener.getClass()。getName(),而不是使用callbackListener.getClass()。toString()。 - kabuko