Android 以JSON格式将上传图像和文本发送到url(服务器)
我有麻烦了。。。我有1个edittext和2个按钮,一个用于发布,另一个用于上传相机图像和在imageview中显示图像。我的代码没有错误,但当我单击“发布”按钮时,服务器上没有显示图像和文本。请检查代码我是新手,因此我不知道我在哪里丢失了代码。先谢谢你 这是我的PostActivity.java:Android 以JSON格式将上传图像和文本发送到url(服务器),android,image,post,text,server,Android,Image,Post,Text,Server,我有麻烦了。。。我有1个edittext和2个按钮,一个用于发布,另一个用于上传相机图像和在imageview中显示图像。我的代码没有错误,但当我单击“发布”按钮时,服务器上没有显示图像和文本。请检查代码我是新手,因此我不知道我在哪里丢失了代码。先谢谢你 这是我的PostActivity.java: public class PostActivity extends Activity {public static final String TAG="url"; private stati
public class PostActivity extends Activity
{public static final String TAG="url";
private static final int CAMERA_REQUEST = 1888;
TextView submit;
ProgressDialog pDialog;
InputStream is;
EditText content;
ImageView imageView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.plus);
submit = (TextView) findViewById(R.id.button7);
content = (EditText) findViewById(R.id.editText2);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new MainTest().execute();
}
});
imageView = (ImageView)findViewById(R.id.imageView9);
ImageButton b = (ImageButton) findViewById(R.id.imageButton5);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
public void postImageData() {
try
{
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.holder);
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://blackispink.com/apis/add_post.php?title="
+ content.getText().toString() + "&content=" + content.getText().toString() + "&cat_id=760" +"&user_id=21");
Log.v(TAG, "postURL: " + postRequest);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
/* example for setting a HttpMultipartMode */
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
try{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();
ByteArrayBody bab = new ByteArrayBody(data, "image.jpg");
builder.addPart("key", bab);
builder.addPart("key4", new StringBody(content.getText().toString(), ContentType.TEXT_PLAIN));
builder.addPart("key1", new StringBody(content.getText().toString(), ContentType.TEXT_PLAIN));
builder.addPart("key2", new StringBody("21", ContentType.TEXT_PLAIN));
builder.addPart("key3", new StringBody("760", ContentType.TEXT_PLAIN));
HttpEntity entity = builder.build();
}
catch(Exception e){
//Log.v("Exception in Image", ""+e);
}
postRequest.setEntity((HttpEntity) builder);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
}catch(Exception e){
e.getStackTrace();
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
public class MainTest extends AsyncTask<String, Integer, String> {
@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(PostActivity.this);
pDialog.setMessage("Loading..");
pDialog.setIndeterminate(true);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
postImageData();
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
// data=jobj.toString();
pDialog.dismiss();
}
}
}
公共类PostActivity扩展活动
{public static final String TAG=“url”;
专用静态最终int摄像机_请求=1888;
文本视图提交;
ProgressDialog;
输入流为;
编辑文本内容;
图像视图图像视图;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.plus);
提交=(文本视图)findViewById(R.id.button7);
content=(EditText)findViewById(R.id.editText2);
submit.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
新建MainTest().execute();
}
});
imageView=(imageView)findViewById(R.id.imageView9);
ImageButton b=(ImageButton)findViewById(R.id.imageButton5);
b、 setOnClickListener(新视图。OnClickListener(){
@凌驾
公共void onClick(视图v){
Intent cameraIntent=newintent(android.provider.MediaStore.ACTION\u IMAGE\u CAPTURE);
startActivityForResult(摄像机帐篷、摄像机请求);
}
});
}
public void postImageData(){
尝试
{
位图bitmapOrg=BitmapFactory.decodeResource(getResources(),R.drawable.holder);
HttpClient HttpClient=新的DefaultHttpClient();
HttpPostRequest=新的HttpPost(“http://blackispink.com/apis/add_post.php?title="
+content.getText().toString()+”&content=“+content.getText().toString()+”&cat_id=760“+”&user_id=21”);
Log.v(标签“postrl:+postRequest”);
MultipartEntityBuilder=MultipartEntityBuilder.create();
/*设置HttpMultipartMode的示例*/
builder.setMode(HttpMultipartMode.BROWSER_兼容);
试一试{
ByteArrayOutputStream bos=新建ByteArrayOutputStream();
compress(Bitmap.CompressFormat.JPEG,75,bos);
字节[]数据=bos.toByteArray();
ByteArrayBody bab=新的ByteArrayBody(数据,“image.jpg”);
建造商。添加零件(“键”,bab);
addPart(“key4”,新的StringBody(content.getText().toString(),ContentType.TEXT_-PLAIN));
addPart(“key1”,新的StringBody(content.getText().toString(),ContentType.TEXT_-PLAIN));
builder.addPart(“key2”,新的StringBody(“21”,ContentType.TEXT_-PLAIN));
builder.addPart(“key3”,新的StringBody(“760”,ContentType.TEXT_-PLAIN));
HttpEntity=builder.build();
}
捕获(例外e){
//Log.v(“图像中的异常”,“e”);
}
setEntity((HttpEntity)生成器);
HttpResponse response=httpClient.execute(postRequest);
BufferedReader=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent(),“UTF-8”);
字符串响应;
StringBuilder s=新的StringBuilder();
而((sResponse=reader.readLine())!=null){
s=s.append(sResponse);
}
}捕获(例外e){
e、 getStackTrace();
}
}
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
if(requestCode==CAMERA\u请求和&resultCode==Activity.RESULT\u确定){
位图照片=(位图)数据.getExtras().get(“数据”);
设置图像位图(照片);
}
}
公共类MainTest扩展了异步任务{
@凌驾
受保护的void onPreExecute(){
pDialog=新建进度对话框(PostActivity.this);
pDialog.setMessage(“加载…”);
pDialog.setUndeterminate(真);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
postImageData();
返回null;
}
@凌驾
受保护的void onPostExecute(字符串结果){
//TODO自动生成的方法存根
super.onPostExecute(结果);
//data=jobj.toString();
pDialog.disclose();
}
}
}
将图像转换为Base 64并将其发送到服务器。将图像转换为Base 64并将其发送到服务器。对于文本,可以使用代码行
HttpClient client = new DefaultHttpClient();
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("key1","content 1"));
formparams.add(new BasicNameValuePair("key2", "content 2"));
UrlEncodedFormEntity entity;
try {
entity = new UrlEncodedFormEntity(formparams, "UTF-8");
HttpPost httppost = new HttpPost("url link");
httppost.setEntity(entity);
try {
client.execute(httppost);
} catch (ClientProtocolException e) {exception handler }
catch (IOException e) {exception handler }
} catch (UnsupportedEncodingException e) {exception handler }
HttpClient=newdefaulthttpclient();
List formparams=new ArrayList();
formparams.add(新的BasicNameValuePair(“键1”、“内容1”);
formparams.add(新的BasicNameValuePair(“键2”、“内容2”);
UrlEncodedFormEntity实体;
试一试{
实体=新的UrlEncodedFormEntity(formparams,“UTF-8”);
HttpPost HttpPost=新的HttpPost(“url链接”);
httppost.setEntity(实体);
试一试{
执行(httppost);
}catch(ClientProtocolException e){exception handler}
catch(IOException e){exception handler}
}catch(UnsupportedEncodingException e){exception handler}
对于文本,您可以使用代码行
HttpClient client = new DefaultHttpClient();
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("key1","content 1"));
formparams.add(new BasicNameValuePair("key2", "content 2"));
UrlEncodedFormEntity entity;
try {
entity = new UrlEncodedFormEntity(formparams, "UTF-8");
HttpPost httppost = new HttpPost("url link");
httppost.setEntity(entity);
try {
client.execute(httppost);
} catch (ClientProtocolException e) {exception handler }
catch (IOException e) {exception handler }
} catch (UnsupportedEncodingException e) {exception handler }
HttpClient=newdefaulthttpclient();
List formparams=new ArrayList();