public class MainActivity extends ActionBarActivity {
public Bitmap bitmap;
public static String exsistingFileName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("Tag", "First Log Message");
InputStream is=getResources().openRawResource(R.drawable.ic_launcher);
bitmap = BitmapFactory.decodeStream(is); //This gets the image
ImageView myImageView = (ImageView)findViewById(R.id.myImageView);
myImageView.setImageBitmap(bitmap);
try {
sendData(null);
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendData(String[] args) throws Exception {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost("http://localhost/xampp/UploadToServer.php");
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
Log.d("Tag", "Second Log Message");
// format of image compressed - 640 x 480;
Bitmap bmpCompressed = Bitmap.createScaledBitmap(bitmap, 640, 480, true);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Log.d("Tag", "Third Log Message");
bmpCompressed.compress(CompressFormat.JPEG, 100, bos);
Log.d("Tag", "Fourth Log Message");
byte[] data = bos.toByteArray();
entity.addPart("myParam", new StringBody("my value"));
entity.addPart("myImage", new ByteArrayBody(data, "temp.jpg"));
Log.d("Tag", "Fifth Log Message");
httpPost.setEntity(entity);
Log.d("Tag", "Sixth Log Message");
HttpResponse response = httpClient.execute(httpPost,localContext);
Log.d("Tag", "Seventh Log Message");
BufferedReader reader = new BufferedReader(new InputStreamReader( response.getEntity().getContent(), "UTF-8"));
String sResponse = reader.readLine();
} catch (Exception e) {
System.out.println(e.getClass().getName());
Log.v("myApp", "Some error came up");
}
}
}
它没有在"响应"中给出任何价值。 以下是与此活动相关的 logcat 信息。
06-22 03:31:39.273:D / Tag(2889):First Log Message
06-22 03:31:39.289:D / Tag(2889):第二个日志消息
06-22 03:31:39.414:D / Tag(2889):第三个日志消息
06-22 03:31:39.539:D / Tag(2889):第四个日志消息
06-22 03:31:39.546:D / Tag(2889):第五个日志消息
06-22 03:31:39.546:D / Tag(2889):第六个日志信息
06-22 03:31:41.304:I / System.out(2889):org.apache.http.conn.HttpHostConnectException
06-22 03:31:41.304:D / Tag(2889):出现了一些错误