我正在做一个可以上传文件的应用程序功能。
完成所有调试并启动程序后
LogCat 没有显示任何有关错误的信息。
但它只是一个空白的白页。
请帮忙,我不知道发生了什么。
爪哇代码:
package com.example.adc;
import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private void doMultiPost(String url, List<NameValuePair> params){
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
try{
//setup multipart entity
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
for(int i=0;i< params.size();i++){
//identify param type by Key
if(params.get(i).getName().equals("file")){
File f=new File(params.get(i).getValue());
FileBody fileBody=new FileBody(f);
entity.addPart("image"+i,fileBody);
}else{
entity.addPart(params.get(i).getName(),new StringBody(params.get(i).getValue(), Charset.forName("UTF-8")));
}
}
post.setEntity(entity);
//create response handler
ResponseHandler <String> handler=new BasicResponseHandler();
//execute and get response
String response=new String(client.execute(post,handler).getBytes(),HTTP.UTF_8);
Toast.makeText(this, response,Toast.LENGTH_SHORT).show();
}catch(Exception e){
e.printStackTrace();
}
}
public void onSubmit(View v){
String path = ((EditText)findViewById(R.id.filePath)).getText().toString();
String des = ((EditText)findViewById(R.id.desTxt)).getText().toString();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("file",path));
params.add(new BasicNameValuePair("des",des));
doMultiPost("10.0.0.2/MultipartPost.php",params);
}
}
显现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.adc"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_OWNER_DATA" />
<uses-permission android:name="android.permission.WRITE_OWNER_DATA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.adc.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>
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:text="file Description:"/>
<EditText
android:id="@+id/desTxt"
android:layout_width="160dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:text="File path:"/>
<EditText
android:id="@+id/filePath"
android:layout_width="160dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
<Button
android:id="@+id/submitBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:onClick="onSubmit"
/>
</LinearLayout>