【发布时间】:2014-08-01 15:01:00
【问题描述】:
我希望从 iOS 应用程序向 PHP 文件发出 POST http 请求。我只需要获取一个字段中的值并将其发布到 PHP。 android 版本尚未完成,但至少可以运行,代码如下:
private class MyAsyncTask extends AsyncTask<String, Integer, Double>{
@Override
protected Double doInBackground(String... params) {
// TODO Auto-generated method stub
postData(params[0]);
return null;
}
protected void onPostExecute(Double result){
Toast.makeText(getApplicationContext(), "Check In Sent!", Toast.LENGTH_SHORT).show();
}
public void postData(String phone) {
String content = "";
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("MY_URL_HERE");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("phone", phone));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
// Custom code for response
StatusLine statusLine = response.getStatusLine();
// Check the HTTP resquest for success
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// toast here
Toast.makeText(getApplicationContext(), "Check in Complete", Toast.LENGTH_LONG).show();
}
});
} else if (statusLine.getStatusCode() == HttpStatus.SC_FORBIDDEN) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// toast here
Toast.makeText(getApplicationContext(), "Number already checked in", Toast.LENGTH_LONG).show();
}
});
} else if (statusLine.getStatusCode() == HttpStatus.SC_BAD_REQUEST); {
runOnUiThread(new Runnable() {
@Override
public void run() {
// toast here
Toast.makeText(getApplicationContext(), "Phone number not found", Toast.LENGTH_LONG).show();
}
});
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
Log.e("ALFA", "HTTPReq:ClientProtocolException " + e.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("ALFA", "HTTPReq:IOException " + e.toString());
}
}
}
没什么太花哨的,我只是很难在 iOS 中找到如何做到这一点。我找到教程并开始做它们,发现它们已被弃用或错误。谁能帮我弄清楚如何做到这一点的基础知识,或者至少是一个关于我如何做到这一点的非过时教程?顺便说一下,这个应用程序适用于 iOS 7。我需要它将电话号码发布到我拥有的 php 文件中,并且 PHP 文件会根据它是否成功返回状态代码。
所以这就是我到目前为止没有第三方库的情况:
NSString *post = [NSString stringWithFormat:@"&phone=%@",@"phonenumberhere"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"url_here"]]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if(conn)
{
NSLog(@"Connection Successful");
}
else
{
NSLog(@"Connection could not be made");
}
现在,如果我检查我的 SQL 数据库,我知道它正在工作并且可以正确发布。但是,我怎样才能看到它返回的状态码呢?
【问题讨论】:
-
更好的提问方式是发布你的objective-c尝试,否则这个问题看起来像:“请为我翻译我的代码”。
-
是的,你是对的!我现在将发布我的尝试。在等待答案时,我让它在某种程度上起作用。我现在将编辑我的帖子以包含它
标签: android ios objective-c