我遇到了麻烦...我有1个edittext和2个按钮,一个用于发布,一个用于上传摄像机图像和在imageview中显示图像。我的代码没有错误但是当我点击发布按钮时图像和文本是没有在服务器上显示请检查代码我是新手所以我不知道我错过了代码。先谢谢你。
这是我的PostActivity.java:
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();
}
}
}
答案 0 :(得分:0)
将您的图片转换为Base 64并将其发送到您的服务器。
答案 1 :(得分:0)
对于文本,您可以使用代码行
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 }