我正在eclipse中开发和应用,它将一个简单的txt文件上传到php服务器。我的问题是,当我尝试调试它时,eclipse透视图会更改并写入:
我的代码是:
package com.example.tryyy;
import java.io.File;
import java.io.FileInputStream;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
String url = "http://www.acliberation.comeze.com/upload.php";
File file = new File(Environment.getExternalStorageDirectory(),
"kutya.txt"); {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
InputStreamEntity reqEntity = new InputStreamEntity(
new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true);
// Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
Toast.makeText(getApplicationContext(), response.toString(),
Toast.LENGTH_LONG).show();
// Do something with response...
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.toString(),
Toast.LENGTH_LONG).show();
}
}
}
我的清单是:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tryyy"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.tryyy.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
服务器端php是:
<?php
$target_path = "./";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
我已经下载了针对API 19的SDK的源代码,但问题仍然存在。
提前感谢您的帮助!
答案 0 :(得分:0)
稍微修改了你的代码。试试吧
package com.example.tryyy;
import java.io.File;
import java.io.FileInputStream;
import java.net.URI;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;
public class MainActivity extends Activity {
private String url = "http://www.acliberation.comeze.com/upload.php";
private String[] params;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
params = new String[2];
params[0] = Environment.getExternalStorageDirectory() + File.separator + "kutya.txt";
params[1] = url;
new HTTPRequest().execute(params);
setContentView(R.layout.activity_main);
}
private class HTTPRequest extends AsyncTask<String, Void, String> {
protected String doInBackground(String... params) {
String returnString = null;
try {
File file = new File(params[0]);
URI uri= new URI(params[1]);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(uri);
InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true);
// Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
returnString = response.toString();
// Do something with response...
} catch (Exception e) {
returnString = e.getMessage();
}
return returnString;
}
protected void onPostExecute(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
}
}