HTTP POST没有反应 [英] http post no response
问题描述
我用后有文件上传图像文件的网站。我得到了错误message.Could你能给我一些建议吗?谢谢
操作系统:Android 4.0.3手机:HTC X
错误信息
E / dalvikvm(11952):找不到类的org.apache.http.entity.mime.content.FileBody,从法tw.tcc.tsvs.www引用。 FileUploadPost.FileUploadPostActivity $ 1.onClick
code
进口java.io.BufferedReader中;
进口的java.io.File;
进口的java.io.InputStream;
进口java.io.InputStreamReader中;
进口org.apache.http.HttpEntity;
进口org.apache.http.Htt presponse;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.methods.HttpGet;
进口org.apache.http.client.methods.HttpPost;
进口org.apache.http.entity.mime.HttpMultipartMode;
进口org.apache.http.entity.mime.MultipartEntity;
进口org.apache.http.entity.mime.content.FileBody;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.apache.http.util.EntityUtils;
进口android.app.Activity;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.TextView;私人Button.OnClickListener jclove =新OnClickListener(){
公共无效的onClick(视图v){
尝试{
// file_post
档案文件=新的文件(/ SD卡/ android.jpg);
尝试{
HttpClient的客户端=新DefaultHttpClient();
字符串postURL =http://latest.tsd2497r1.ext.hipaas.hinet.net/;
HttpPost后=新HttpPost(postURL);
FileBody斌=新FileBody(文件);
MultipartEntity reqEntity =新MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart(MYFILE,斌);
post.setEntity(reqEntity);
HTT presponse响应= client.execute(岗位);
HttpEntity resEntity = response.getEntity();
如果(resEntity!= NULL){
Log.i(反应,EntityUtils.toString(resEntity));
}
}赶上(例外五){
e.printStackTrace();
}
//}
}赶上(例外五){
e.printStackTrace();
}
}
};
从code和错误我想你忘了加<一个href=\"http://www.findjar.com/jar/org/apache/httpcomponents/httpmime/4.0/httpmime-4.0.jar.html?all=true\"相对=nofollow>在构建路径httpmime-4.0.jar 文件。
示例code:
HttpPost httpost =新HttpPost(URL上传文件);MultipartEntity实体=新MultipartEntity();
entity.addPart(myIdentifier,新StringBody(someValue中));
entity.addPart(myAudioFile,新FileBody(文件));httpost.setEntity(实体);
HTT presponse响应;
响应= httpclient.execute(httpost);
看我在这太问题的答案。
<一个href=\"http://stackoverflow.com/questions/7037717/upload-an-image-and-audio-in-one-request-in-android/7038888#7038888\">upload在Android的要求之一的影像和音频
I use post with file to upload a image file to website. i got the error message.Could you give me some suggestion? Thanks
system:android 4.0.3
phone:htc x
error information
E/dalvikvm(11952): Could not find class 'org.apache.http.entity.mime.content.FileBody', referenced from method tw.tcc.tsvs.www.FileUploadPost.FileUploadPostActivity$1.onClick
code
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
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.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
private Button.OnClickListener jclove=new OnClickListener(){
public void onClick(View v) {
try {
//file_post
File file = new File("/sdcard/android.jpg");
try {
HttpClient client = new DefaultHttpClient();
String postURL = "http://latest.tsd2497r1.ext.hipaas.hinet.net/";
HttpPost post = new HttpPost(postURL);
FileBody bin = new FileBody(file);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("myFile", bin);
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
Log.i("RESPONSE",EntityUtils.toString(resEntity));
}
} catch (Exception e) {
e.printStackTrace();
}
//}
} catch (Exception e) {
e.printStackTrace();
}
}
};
From your code and error I think you forgot to add httpmime-4.0.jar file in your build path.
Example Code:
HttpPost httpost = new HttpPost("url for upload file");
MultipartEntity entity = new MultipartEntity();
entity.addPart("myIdentifier", new StringBody("somevalue"));
entity.addPart("myAudioFile", new FileBody(File));
httpost.setEntity(entity);
HttpResponse response;
response = httpclient.execute(httpost);
Look at My answer in this SO question.
upload an image and audio in One request in android
这篇关于HTTP POST没有反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!