【发布时间】:2016-07-11 12:16:54
【问题描述】:
我正在开发一个应用程序,因为我已经开发了用于进行 web 服务调用的 ios 代码,并且它成功地给了我响应,当我尝试在 android 中调用时,相同的 web 服务调用它不起作用它没有给我回复,我正在发布我的两个代码,有人可以帮我弄清楚吗?
ios 代码
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",k_SERVER_BASE_ADDRESS,service_name]];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
// 2
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = @"POST";
[request setValue:[NSString stringWithFormat:@"%@",k_CONTENT_TYPE] forHTTPHeaderField:@"Content-Type"];
// 3
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObject:[NSString stringWithFormat:@"%@",k_USER_NAME] forKey:@"APIUserName"];
[dictionary setObject:[NSString stringWithFormat:@"%@",k_PASSWORD] forKey:@"Password"];
NSLog(@"%@",dictionary);
NSError *error = nil;
NSData *data_prm = [NSJSONSerialization dataWithJSONObject:dictionary
options:kNilOptions error:&error];
NSLog(@"%@",[[NSString alloc] initWithData:data_prm encoding:NSUTF8StringEncoding]);
if (!error) {
// 4
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
fromData:data_prm completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {
// Handle response here..
NSLog(@"this is data : %@",data);
Android 代码
public class GETCONTESTANTS extends AsyncTask<Void, Void, Void> {
StringEntity se;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(StartActivity.this);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
pDialog.show();
// stateList.clear();
}
@Override
protected Void doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.fansplay.com/wsfptb20/FBS.svc/GetContestants");
String json = "";
try {
// 3. build jsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("APIUserName", "AFBK8@DL4EJMd6");
jsonObject.accumulate("country","8GB4HE1C-EFSD-4L17-VY2D-A27OC8C52F6M");
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();
// ** Alternative way to convert Person object to JSON string usin Jackson Lib
// ObjectMapper mapper = new ObjectMapper();
// json = mapper.writeValueAsString(person);
// 5. set json to StringEntity
se = new StringEntity(json);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 6. set httpPost Entity
httpPost.setEntity(se);
// 7. Set some headers to inform server about the type of the content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
在上述代码中,“iOS 代码”工作正常,“Android 代码”不工作。谁能帮帮我?
【问题讨论】:
-
调试您的代码并尝试发布 logcat 输出
-
您是否遇到任何错误/异常情况?
-
你到底得到了什么错误?
-
您可以使用 Retrofit 而不是使用 asyncTask,它是最好的 HTTP 客户端库,而且使用起来非常简单。检查此链接:square.github.io/retrofit
-
我猜 HttpClient 已被弃用。在 AsyncTask 或 Retrofit 中使用 HTTPURLConnection。
标签: java android ios objective-c web-services