这是导入的jar包,在java运行没问题
出错提示java.lang.NoSuchFieldError: No static field INSTANCE of type Lorg/apache/http/message/BasicHeaderValueFormatter; in class Lorg/apache/http/message/BasicHeaderValueFormatter; or its superclasses (declaration of 'org.apache.http.message.BasicHeaderValueFormatter' appears in /system/framework/ext.jar)
附上源代码
package com.example.tew;
import java.io.File;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.net.ParseException;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button send=null;
private static String url ="http://115.159.114.122/getnew";
private static String userid="1001"; //用户id,不是用户名
private static String title="天气"; //标题
private static String text="天气真的不错"; //内容
private static String picNum="2"; //图片数量
private static String classify="修理"; //分类
private static String position="上海市宝山区"; //位置
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send=(Button)findViewById(R.id.button1);
send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
HttpClient client = new DefaultHttpClient();// 开启一个客户端 HTTP 请求
HttpPost post = new HttpPost(url);// 创建 HTTP POST 请求
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);// 设置浏览器兼容模式
ContentType contentType= ContentType.create(HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8);
//添加上传文件,此处选择了两个文件上传,所以picNum的数值是2,1个图片的话picNum的值为1,以此类推
File file1 = new File("C:/Users/cao/Desktop/00.png");
File file2 = new File("C:/Users/cao/Desktop/01.jpg");
builder.addBinaryBody("file", file1);
builder.addBinaryBody("file", file2);
//
builder.addPart("userid", new StringBody(userid,contentType));// 设置请求参数
builder.addPart("title",new StringBody(title,contentType));// 设置请求参数
builder.addPart("text",new StringBody(text,contentType));// 设置请求参数
builder.addTextBody("picNum", picNum);// 设置请求参数
builder.addPart("classify", new StringBody(classify,contentType));// 设置请求参数
builder.addPart("position", new StringBody(position,contentType));// 设置请求参数
HttpEntity entity = builder.build();// 生成 HTTP POST 实体
post.setEntity(entity);// 设置请求参数
try {
HttpResponse response = client.execute(post);// 发起请求 并返回请求的响应
if (response.getStatusLine().getStatusCode() == 200) { //状态码200的时候说明发送成功
System.out.println("发送成功");
System.out.println(EntityUtils.toString(response.getEntity())); //获取服务器返回的值
}else{
System.out.println("发送失败,状态码"+response.getStatusLine().getStatusCode());
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
3 个解决方案
#1
httpClient的包只留一个
#2
楼主解决了吗???
#3
参考 http://blog.csdn.net/davebobo/article/details/52763141
下载 http://download.csdn.net/detail/davebobo/9666003即可解决
下载 http://download.csdn.net/detail/davebobo/9666003即可解决
#1
httpClient的包只留一个
#2
楼主解决了吗???
#3
参考 http://blog.csdn.net/davebobo/article/details/52763141
下载 http://download.csdn.net/detail/davebobo/9666003即可解决
下载 http://download.csdn.net/detail/davebobo/9666003即可解决