这是标题:
ListView listOne;
SimpleAdapter ADA;
ActionBar actionBar;
EditText etSearch;
final Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_global_search);
listOne = (ListView) findViewById(R.id.listView1);
etSearch = (EditText) findViewById(R.id.etSearch);
etSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE
);
}
});
try {
etSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
GlobalSearch.this.ADA.getFilter().filter(etSearch.getText().toString().trim());
}
});
}catch (Exception e){
e.printStackTrace();
}
}
@Override
protected void onPostExecute(String z) {
Toast.makeText(getBaseContext(), z, Toast.LENGTH_SHORT).show();
pDialog.dismiss();
try {
String[] from = {"A","A1","B","C", "D"};
int[] views = {R.id.lbl1,R.id.lbl2,R.id.lbl3,R.id.lbl4, R.id.lblShift};
ADA = new SimpleAdapter(GlobalSearch.this, proList,
R.layout.list_layout_global_search, from, views);
listOne.setAdapter(ADA);
}catch (Exception ex){
ex.printStackTrace();
}
}
}
我尝试在两种情况下解决它:
案例1 (崩溃):
"url": "{{host}}/images/bla",
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"description": ""
},
{
"key": "Authorization",
"value": "Bearer token",
"description": ""
}
],
"body": { ... }
案例2 (不会崩溃,但也不起作用):
NSDictionary *headerDict = @{@"key": @"Content-Type",
@"value": @"application/json",
@"description": @""};
NSDictionary *headerDict1 = @{@"key": @"Authorization",
@"value": kBearerKey,
@"description": @""};
NSArray *headerArray = @[headerDict, headerDict1];
[weakSelf.requestOperationManager.requestSerializer setValue:headerArray forHTTPHeaderField:@"header"];
无论如何,在案例1 中会生成一个警告(并且也会崩溃),因为weakSelf.requestOperationManager.requestSerializer = [AFHTTPRequestSerializer serializer];
[weakSelf.requestOperationManager.requestSerializer setValue:@"Content-Type" forHTTPHeaderField:@"key"];
[weakSelf.requestOperationManager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"value"];
[weakSelf.requestOperationManager.requestSerializer setValue:@"Authorization" forHTTPHeaderField:@"key"];
[weakSelf.requestOperationManager.requestSerializer setValue:kBearerKey forHTTPHeaderField:@"value"];
应该是setValue
而不是NSString
。
答案 0 :(得分:0)
您可以将NSMutableDictionary中的所有标头值保留为键/值对,例如
NSDictionary *headersDict = @{@"Content-Type": @"application/json",
@"Authorization": kBearerKey };
然后您可以迭代键值并设置http标头
[headersDict enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
[requestManager.requestSerializer setValue:obj forHTTPHeaderField:key];
}];
在您的情况下,您正在使用数组..您可以迭代数组并设置标题值,希望这会有所帮助。
for (NSDictionary *dict in headerArray) {
NSString *key = [dict objectForKey:@"key"];
NSString *value = [dict objectForKey:@"value"];
[requestManager.requestSerializer setValue:value forHTTPHeaderField:key];
}
答案 1 :(得分:0)
试试这个;
NSError *error;
NSData *jsonData = [NSJSONSerialization:headerArray options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
[weakSelf.requestOperationManager.requestSerializer setValue:jsonString forHTTPHeaderField:@"header"];
答案 2 :(得分:0)
试试这个:
AFNetworking 3
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject: headerArray options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:url parameters:nil error:nil];
req.timeoutInterval= [[[NSUserDefaults standardUserDefaults] valueForKey:@"timeoutInterval"] longValue];
[req setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[req setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[req setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
uploadTaskWithStreamedRequest:req
progress:^(NSProgress * _Nonnull uploadProgress) {
dispatch_async(dispatch_get_main_queue(), ^{
});
}
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"Error: %@", [[NSString alloc]initWithData:[[error valueForKey:@"userInfo"] valueForKey:@"com.alamofire.serialization.response.error.data"] encoding:NSUTF8StringEncoding]);
} else {
}
}];
[uploadTask resume];
答案 3 :(得分:0)
NSDictionary *headerDict = @{@"Content-Type": @"application/json", @"Authorization": kBearerKey};
OR
[weakSelf.requestOperationManager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[weakSelf.requestOperationManager.requestSerializer setValue:kBearerKey forHTTPHeaderField:@"Authorization"];