i am writing a small app which uploads a picture when a taken a picture from camera. the following is the complete code. When i install this on my phone and run the app i don't see any thing in the apache tomcat logs(i.e post request is not not able to execute).
I tested the back end with web browser on my phone and it works. But when i am trying with app i don't see any output on phone as well as in the server logs.
Can anyone tell me if there is any problem in the code? If not how to proceed with these type of errors.
Edited code
public class MainActivity extends Activity {
int REQUEST_CODE = 1;
ImageView IMG;
Bitmap bitmap;
TextView link;
Bitmap bm[] = new Bitmap [4];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (i.resolveActivity(getPackageManager()) != null) {
startActivityForResult(i, REQUEST_CODE);
}
setContentView(R.layout.activity_main);
IMG = (ImageView) findViewById(R.id.img);
link = (TextView) findViewById(R.id.response);
}
public void onActivityResult(int requestcode, int resultcode, Intent data) {
if (requestcode == REQUEST_CODE) {
if (resultcode == RESULT_OK) {
Bundle bundel = new Bundle();
bundel = data.getExtras();
bitmap = (Bitmap) bundel.get("data");
bm[0] = bitmap;
UploadPic picUpload = new UploadPic();
picUpload.execute(bm);
IMG.setImageBitmap(bitmap);
}
}
}
private class UploadPic extends AsyncTask<Bitmap, Integer, String> {
@Override
protected String doInBackground(Bitmap... bitmap) {
// TODO Auto-generated method stub
BufferedReader in = null;
String data = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.185:8080/ver0_2/query");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
/* example for setting a HttpMultipartMode */
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap[0].compress(CompressFormat.JPEG, 75, byteArrayOutputStream);
// bitmap.compress
byte [] byteData = byteArrayOutputStream.toByteArray();
// String strData = Base64.encodeToString(data, Base64.DEFAULT); // I have no idea why Im doing this
ByteArrayBody byteArrayBody = new ByteArrayBody(byteData, "image"); // second parameter is the name of the image
/* example for adding an image part */
builder.addPart("fileUpload", byteArrayBody);
HttpEntity entity = builder.build();
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.seperator");
while ((l = in.readLine()) != null) {
sb.append(l + nl);
}
in.close();
data = sb.toString();
}
catch (Exception e) {
e.printStackTrace();
}
return data;
}
@Override
protected void onPostExecute(String result) {
// execution of result of Long time consuming operation
link.setText(result);
}
}
}