Php 在android服务器上上传db文件
我正在从我的应用程序数据目录上载db文件。服务器总是返回错误消息。我不知道原因。请帮帮我 以下是我的android代码:Php 在android服务器上上传db文件,php,android,http-post,Php,Android,Http Post,我正在从我的应用程序数据目录上载db文件。服务器总是返回错误消息。我不知道原因。请帮帮我 以下是我的android代码: String URL="http://hazelrink.com/apps/Sandbox/upload1.php"; String databasePath = getActivity().getApplicationContext().getDatabasePath("4C1A_14.db").getPath(); File f = new
String URL="http://hazelrink.com/apps/Sandbox/upload1.php";
String databasePath = getActivity().getApplicationContext().getDatabasePath("4C1A_14.db").getPath();
File f = new File(databasePath);
HttpClient c = new DefaultHttpClient();
HttpPost p = new HttpPost(URL);
try {
InputStreamEntity reqEntity = new InputStreamEntity(
new FileInputStream(f), -1);
reqEntity.setContentType("multipart/form-data");
reqEntity.setChunked(true); //
ResponseHandler<String> responseHandler = new BasicResponseHandler();
p.setEntity(reqEntity);
String r= c.execute(p,responseHandler);
status = r;
Log.i("Login", status);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
您是否已在创建此web服务的位置创建了
4CA1\u BR
此名称文件夹???否。我为什么要创建该文件夹?因为基本上这就是.db将要上载的目录。只需在服务器上创建相同名称的文件夹,就在您的web服务所在的位置。我已为此共享了iPhone代码。请确认文件夹是必需的。您的iPhone代码是否正在将文件上载到服务器?
<?php
$uploaddir = '4CA1_BR/';
$file = basename($_FILES['uploadedfile']['name']);
$uploadfile = $uploaddir . $file;
if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $uploadfile)) {
echo "SU";
}
else {
echo "ER";
}
?>
NSDate *currDate = [NSDate date]; //current date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd.MM.YY-HH:mm:ss"]; //format it
dateString = [dateFormatter stringFromDate:currDate]; //save it an a variable
fileName = [NSString stringWithFormat:@"%@_%@", [appDelegate username], dateString]; //name of the database file that is going to be uploaded to the server
//this part
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSString *databaseStringer;
for (NSString *s in fileList){
databaseStringer = s;
}
NSString *path = [documentsDirectory stringByAppendingPathComponent:databaseStringer];
NSData *dataBaseData = [NSData alloc];
dataBaseData = [NSData dataWithContentsOfFile:path];
//this part //this part above (between the blue text) explains how it gets the database and convert it into bytes and store it in a temporary variable
NSString *urlString = [NSString stringWithFormat:@"http://hazelrink.com/apps/Sandbox/upload1.php”]; // the Synchro URL
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"]; //set up the POST method
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; //%@ in objective-C refers to string so in this case the boundry string is included in the contenttype string
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
//the above code is included as a header when doing POSTing. Take note of the value and header
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"%@.db\"\r\n",fileName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:dataBaseData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
//take note of what is being passed in the body
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
//result are below. there is only two result. if ‘SU’ then proceed to doing another method.
if ([returnString isEqualToString:@"ER"]){
myAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Unexpected Error Occured!"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[myAlertView show];
}
else if ([returnString isEqualToString:@"SU"]){
[self writerSQL]; //second part
}